Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


How do I generate a random number in C++ with a set range?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 WhoStoleMyNick   Members   -  Reputation: 122

Like
Likes
Like

Posted 30 July 1999 - 10:58 PM

Hello, I'm making a program in C++ that has to generate a random number within a set range. (the bottom number could be higher than 0) How do I do it?

------------------
|
|/
|\evin Clancy


Sponsor:

#2 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 16 July 1999 - 06:22 PM

Well you would first want to include
srand(time(NULL));
to seed you number in you initialization and then whenever you want a new number you would call
int newrandomnum = rand() % num;
This will produce a number from 0 to num - 1.
You have to #include <time.h> for time and I think stdlib.h for the rand() though I'm not to sure.
Big Daddy Scat

#3 TANSTAAFL   Moderators   -  Reputation: 1152

Like
Likes
Like

Posted 17 July 1999 - 09:56 AM

int RandRange(int lo,int hi)
{
return(rand()%(hi-lo+1)+lo);
}

#4 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 19 July 1999 - 05:51 PM

2+2=4

#5 Darvith   Members   -  Reputation: 122

Like
Likes
Like

Posted 22 July 1999 - 04:03 AM

If you want to generate a random integer between 1 and 10, you should always do it by

j=1+(int) (10.0*rand()/(RAND_MAX+1.0));


the rand()%x function only uses lower-order bits and will generate a much LESS random number( Numbers are never completely random in computers ) then the first method.

Yeah it seems much more complex, but just through it in a function.

On the otherhand if you don't really care how random the numbers really are and thats not important to you, the simpler way will work.

------------------
Gary
Midnights Dawning Software
http://dawning.dhs.org




#6 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 30 July 1999 - 10:55 PM

Darvith is right, and most good manuals will suggest the floating point random number method.

The following macro will return a random number between min(inclusive) and max(exclusive):


#define randomRange(min, max) ( (int)(min + (max-min)*rand()/(RAND_MAX+1.0)) )

randomRange(1, 10) will then return a random integer between 1 and 10.
You will also need to #include and call srand(time(NULL)); as mentioned by others. #include is not strictly necessary, since you are not using the time_t structure in the call to time().

Regards,

White Fire


#7 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 30 July 1999 - 10:58 PM

That is, you will need to #include stdlib.h, but time.h is not strictly necessary.

White Fire





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS