rand() range help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 30
Reputation: cl3m0ns is an unknown quantity at this point 
Solved Threads: 0
cl3m0ns's Avatar
cl3m0ns cl3m0ns is offline Offline
Light Poster

rand() range help

 
0
  #1
Nov 11th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 16
Reputation: maxmaxwell is an unknown quantity at this point 
Solved Threads: 1
maxmaxwell maxmaxwell is offline Offline
Newbie Poster

Re: rand() range help

 
0
  #2
Nov 11th, 2007
  1. <span class="ad_notxt"><code class="inlinecode">
  2. int high=10;
  3. int low=6;
  4. rand () % (high - low + 1) + low;
  5. </code></span>



hope that helps.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 30
Reputation: cl3m0ns is an unknown quantity at this point 
Solved Threads: 0
cl3m0ns's Avatar
cl3m0ns cl3m0ns is offline Offline
Light Poster

Re: rand() range help

 
0
  #3
Nov 11th, 2007
thanks you maxmaxwell
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 47
Reputation: joshua.tilson is an unknown quantity at this point 
Solved Threads: 0
joshua.tilson's Avatar
joshua.tilson joshua.tilson is offline Offline
Light Poster

Re: rand() range help

 
0
  #4
Nov 12th, 2007
Originally Posted by maxmaxwell View Post
  1. <span class="ad_notxt"><code class="inlinecode">
  2. int high=10;
  3. int low=6;
  4. rand () % (high - low + 1) + low;
  5. </code></span>
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!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,614
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: rand() range help

 
0
  #5
Nov 12th, 2007
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 47
Reputation: joshua.tilson is an unknown quantity at this point 
Solved Threads: 0
joshua.tilson's Avatar
joshua.tilson joshua.tilson is offline Offline
Light Poster

Re: rand() range help

 
0
  #6
Nov 12th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC