C++ Random Numbers

Please support our C++ advertiser: Intel Parallel Studio Home
Closed Thread

Join Date: Oct 2007
Posts: 11
Reputation: AnthIste is an unknown quantity at this point 
Solved Threads: 0
AnthIste's Avatar
AnthIste AnthIste is offline Offline
Newbie Poster

Re: C++ Random Numbers

 
0
  #31
Oct 21st, 2007
and let's say i have a range from 2 to N, what should i do to get the output only from 2 to N and not 0 to N?because what's given in the previous thread is a function to choose from 0 to N.
thank you
Say you want a random number from 2 to n where n is 15, you would do the following:

  1. int n = 15;
  2. int random_int;
  3.  
  4. random_int = (rand()%n-2)+2;

lets look at the min/max scenarios.

MIN:
Should be 2 (right?)
so you random 0. But you said +2 at the end. so you get 2. which is minimum.

MAX:
should be 15. But max of (15-2) is 13. But you added 2. So its 15.

Problem solved
=======================
I CANT GET AN IMAGE IN MY SIG :(
=======================
Quick reply to this message  
Closed Thread

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