944,026 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 12134
  • C++ RSS
Nov 11th, 2007
0

rand() range help

Expand Post »
I have int x

I would like to fill it with a randomly generated number between lets say 6 and 10

I know that x = 1 + rand() % maxRange; will limit the maximum range but i would also like to limit the minimum range.

How do i put a minRange on the rand() feature

any help would be great

Thanks
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
cl3m0ns is offline Offline
30 posts
since Oct 2007
Nov 11th, 2007
0

Re: rand() range help

C++ Syntax (Toggle Plain Text)
  1. <code class="inlinecode">
  2. int high=10;
  3. int low=6;
  4. rand () % (high - low + 1) + low;
  5. </code>



hope that helps.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
maxmaxwell is offline Offline
16 posts
since Nov 2007
Nov 11th, 2007
0

Re: rand() range help

thanks you maxmaxwell
Reputation Points: 10
Solved Threads: 0
Light Poster
cl3m0ns is offline Offline
30 posts
since Oct 2007
Nov 12th, 2007
0

Re: rand() range help

Click to Expand / Collapse  Quote originally posted by maxmaxwell ...
C++ Syntax (Toggle Plain Text)
  1. <code class="inlinecode">
  2. int high=10;
  3. int low=6;
  4. rand () % (high - low + 1) + low;
  5. </code>
Hello,

I was hoping some one could shed some light on the reason this works. I have used rand() in the past to simulate rolling dice (1-100 etc.). I have never used the minimum number like this. why is it that rand() % 11 = a range of 6 - 10?

Thank you!
Reputation Points: 13
Solved Threads: 0
Light Poster
joshua.tilson is offline Offline
47 posts
since Nov 2007
Nov 12th, 2007
0

Re: rand() range help

>I was hoping some one could shed some light on the reason this works.
Think of it as a normal range shifted to fit your lower bound. When you use x % y, it forces x to be within the range of 0 to y. This corresponds to rand() % ( high - low + 1 ) , or rand() % 5 . So now you have a number from 0 to 5 (excluding 5), but you really want a number from 6 to 11 (excluding 11). So you add the lowest number in your range, which is 6. This effectively shifts the range to what you want by changing the normal lower bound to the lower bound that you want.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 12th, 2007
0

Re: rand() range help

Thank you Narue,

I knew you would come to my rescue. It makes much more sense to me now. I think i was just grappling with the unseen mathematics behind it.
Reputation Points: 13
Solved Threads: 0
Light Poster
joshua.tilson is offline Offline
47 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: New in C++ Help with Arrays
Next Thread in C++ Forum Timeline: convert integer to array of character???





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC