Random numbers in C++

Started by
10 comments, last by Jeff D 22 years, 7 months ago
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
Suffered seven plagues, but refused to let the slaves go free. ~ Ross Atherton
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
thx so is there something liek Qbasic''s RANDOMIZE TIMER?
can you use the time to seed the generator?



Thx,

Jeff Desrosiers
Suffered seven plagues, but refused to let the slaves go free. ~ Ross Atherton
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
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).
quote:Original post by Oluseyi
srand(time());  


That should actually be:
  srand(time(NULL));  

It''s just a typo, I know .

[Resist Windows XP''s Invasive Production Activation Technology!]
Also: time is an ANSI standard function, it''s the same under every compiler

[Resist Windows XP''s Invasive Production Activation Technology!]
quote:Original post by Null and Void
...srand(time(NULL));


Thanks, pard.
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
Suffered seven plagues, but refused to let the slaves go free. ~ Ross Atherton
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++!

This topic is closed to new replies.

Advertisement