• Advertisement

Archived

This topic is now archived and is closed to further replies.

Random numbers in C++

This topic is 5948 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I would like to know how to make random numbers in C++. Im in my 2nd week in C++ and im almost done my first book in it. Just the book never shown me this. Thx, Jeff Desrosiers

Share this post


Link to post
Share on other sites
Advertisement
C/C++ does not have have a built in random number generator, as in it isn't part of the language. You can however use a precreated random number generator function that should be included with your compiler.

Try this in your source code:
  
#include <stdlib.h>

You can "seed" the random number generator with this function:
  
void srand ( unsigned int seed );

and you can get the next random number in the random number sequence with this function:
  
int rand ( void );

Here is how you'd use these methods:
  
int index = 0;
srand( 4 );
for( index = 0; index < 10; index++)
{
cout << rand() << endl;
}

This source code will seed the random number generator and then return ten random numbers and output them to standard out.

One thing to remember about random numbers on computers is that they aren't truely random. If you were to seed the random number generator with the value seed=2 and then call rand 12 times and you were to then reseed the random number generator to seed=2 again and call rand 12 times the values you'd get from rand would be the same for both times.

These functions for random number generation work in linux/unix so I'm not sure if they works in windows. I'm pretty sure that stdlib.h is the same for both though, so you can give it a shot.

RandomTask

Edited by - RandomTask on October 10, 2001 4:28:26 PM

Share this post


Link to post
Share on other sites
thx so is there something liek Qbasic''s RANDOMIZE TIMER?
can you use the time to seed the generator?



Thx,

Jeff Desrosiers

Share this post


Link to post
Share on other sites
You most definitely can seed the random number generator with the time. All operating systems should have some sort of function to return the system time. If you are on a linux/unix machine type "man 2 time" or "man time" on a command line to get information. If you are on a windows box do a help search for "time".

RandomTask

Share this post


Link to post
Share on other sites
yes, the Windows and Linux rand() functions are identical.

Windows seeding with time:

srand(time());


The time() function is declared in time.h and returns the system time in seconds. I believe it''s the same under Linux (rusty; haven''t programmed Linux in a while).

Share this post


Link to post
Share on other sites
Thx a lot guys Im learning slowly but smoothly I just got inheritence down to par now

Ok this is nice now for a little harder question Ive been noticing that it makes big numbers now lets say I wanted it to make a random number from 1 to 100 I know it can be done.
Kinda like Qbasics num = INT(RND * 100) + 1

if you can answer this I will be most grateful

thx,
Jeff Desrosiers

Edited by - Jeff D on October 10, 2001 10:37:29 PM

Share this post


Link to post
Share on other sites
This is how you do it (there are more advanced methods, but this is sufficient for your purposes I believe).

// Create your variable and intialize it to 0
int randomNumber = 0;

// Seed random nuber generator so you don''t constantly get the
// Same sequence of random numbers each time you run the program
srand(time(NULL));

// rand % 100 limits it from 0 to 99
// Add one to get 1 to 100
randomNumber = (rand() % 100) + 1;

Pretty neat, eh?
Good luck and have fun learning how to program in C++!

Share this post


Link to post
Share on other sites

  • Advertisement