Random Number Help.

closed account (967L1hU5)
Simple question, I'm making a Blackjack sorta game, where you can get negatives as well as positives. How would I get C++ to give me numbers -21 threw 21? Preferably on a template as such:
h1 = rand() % 21 + 1;
(But that's only 1-21)
Any help would be appreciated!
Last edited on
rand returns numbers in range [0..??]. You can map those numbers to a range [0..(n-1)] using rand()%n. If you don't want the range to start at 0, you can add some k and get a range [k..(n-1+k)].
If the range you want is [-21..21], can you see what n and k are supposed to be?
Topic archived. No new replies allowed.