By using this site, you agree to our Privacy Policy and our Terms of Use. Close

General - Huh, 6, why 6??? - View Post

I just did a basic search for "C++ Random Number" in google and got this srand()



The pseudo random number generator produces a sequence of numbers that
gives the appearance of being random, when in fact the sequence will
eventually repeat and is predictable.



We can seed the generator with the srand() function. This will start
the generator from a point in the sequence that is dependent on the
value we pass as an argument. If we seed the generator once with a
variable value, for instance the system time, before our first call of
rand() we can generate numbers that are random enough for simple use
(though not for serious statistical purposes).



In our earlier example the program would have generated the same number
each time we ran it because the generator would have been seeded with
the same default value each time. The following code will seed the
generator with the system time then output a single random number,
which should be different each time we run the program.   #include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int main()
{
srand((unsigned)time(0));
int random_integer = rand();
cout << random_integer << endl;
}



Seppukuties is like LBP Lite, on crack. Play it already!

Currently wrapped up in: Half Life, Portal, and User Created Source Mods
Games I want: (Wii)Mario Kart, Okami, Bully, Conduit,  No More Heroes 2 (GC) Eternal Darkness, Killer7, (PS2) Ico, God of War1&2, Legacy of Kain: SR2&Defiance


My Prediction: Wii will be achieve 48% market share by the end of 2008, and will achieve 50% by the end of june of 09. Prediction Failed.

<- Click to see more of her