• Create Account

Random numbers in C++

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.

11 replies to this topic

#1Jeff D  Members

122
Like
Likes
Like

Posted 10 October 2001 - 09:08 AM

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

122
Like
Likes
Like

Posted 10 October 2001 - 09:20 AM

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

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.

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

#3Jeff D  Members

122
Like
Likes
Like

Posted 10 October 2001 - 09:27 AM

thx so is there something liek Qbasic''s RANDOMIZE TIMER?
can you use the time to seed the generator?

Thx,

Jeff Desrosiers

122
Like
Likes
Like

Posted 10 October 2001 - 09:32 AM

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".

#5Oluseyi  GDNet+

2076
Like
Likes
Like

Posted 10 October 2001 - 09:46 AM

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).

#6Null and Void  Members

1087
Like
Likes
Like

Posted 10 October 2001 - 09:56 AM

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!]

#7Null and Void  Members

1087
Like
Likes
Like

Posted 10 October 2001 - 10:00 AM

Also: time is an ANSI standard function, it''s the same under every compiler

[Resist Windows XP''s Invasive Production Activation Technology!]

#8Oluseyi  GDNet+

2076
Like
Likes
Like

Posted 10 October 2001 - 10:02 AM

quote:
Original post by Null and Void
...srand(time(NULL));

Thanks, pard.

#9Jeff D  Members

122
Like
Likes
Like

Posted 10 October 2001 - 03:32 PM

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

#10Viscous-Flow  Members

122
Like
Likes
Like

Posted 10 October 2001 - 03:45 PM

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++!

#11Jeff D  Members

122
Like
Likes
Like

Posted 10 October 2001 - 03:49 PM

LOL I cant I didn''t think of that thanks man

#12Viscous-Flow  Members

122
Like
Likes
Like

Posted 10 October 2001 - 03:55 PM