0

Possible Duplicate:
Generating random integer from a range

I am just starting to learn c++, and for a class assignment i'm supposed to use rand() and get a random number between 2 and 14. I know to get like between 1 and 10 i would do rand()%10+1

In my case would I need to do rand()%14+2?

I understand that others have posted similar questions, but they appear to all do between even multiples, such as 1 and 10, or 10 and 20 etc.

Community
  • 1
  • 1
art3m1sm00n
  • 389
  • 6
  • 9
  • 19
  • I'd try doing %12 instead of %14. – duffymo Jan 31 '13 at 00:29
  • 2
    Do *do* know that `rand()%10+1` doesn't give **uniformly distributed** random numbers in the range between 1 and 10, right? Some integers are more likely to come out than others. The same applies to `rand()%14+2` and the variant suggested in a comment. – Celada Jan 31 '13 at 00:31
  • Please don't remove the automatically inserted duplicate link. – Dennis Jan 31 '13 at 01:16

1 Answers1

0

rand()%12+2 notice there is a gap of 12 number in between and your lower bound is 2.

CoderDake
  • 1,497
  • 2
  • 15
  • 30