Hi, all!
I really need a good random number generator for a baseball simulator (nothing fancy, just playing with probabilities). Currently I'm using the rand() function, and I've made a little test program to see after how many generations will the "random numbers" repeat themselves, and the results look promising. After 100 tests I get a mean value of 1'803'626'901 un-repeated numbers, which is great. Below is the program I used to get this statistic:
(don't worry, the while loop takes more than a second so there won't be two identical seeds - on my dual core T2390)Code:#include <iostream> #include <cstdlib> using namespace std; int main() { long unsigned int rand_nr = 0, rand_nr_new; long unsigned int i = 0, sum = 0; for(int j = 1; j <= 100; j++) { srand(time(NULL)); i = 0; rand_nr_new = rand(); while(rand_nr != rand_nr_new) { rand_nr = rand_nr_new; rand_nr_new = rand(); i += 1; } sum += i; } cout << sum/100 << " iteratii." << endl; }
Now, I should probably tell you how the simulator works. There is a class Team that generates the teams (it uses rand() to generate last season's AVG, SLG, SB% and ERA for the pitcher). Then there is a class Game, that simulates one game (its constructor is Game(Team *home, Team *away)). The game uses a function at_bat to see if the AB was successful for every player (it is a function that depends on the last year AVG and uses rand()). If the at_bat was successful, the hit function gets called to see what the player achieved (single, double, triple or homerun). For this, it uses a function dependent the last year SLG statistic of the player, and of course the rand() function. Add to this that a player may also try to steal a base (based on last year SB% - will try only if SB% is over .750) - another two rand()s, and you'll get a picture of why I need a more real random number generator (there will be over half a million rand()s in every season).
There are 30 teams, each playing 162 games. My solution was to seed the random generator every time before a rand() call, with a function that made the program wait a second before srand. The results are encouraging, but for 162 games between 2 teams, I had to wait about 11'000 seconds (about 3h). The generation of two teams alone takes about 40 seconds.
The next idea, that I'm implementing right now, is to seed not just with srand(time(NULL)), but with srand(time(NULL)+clock()) after I make the program wait for 1/100 seconds. That will get my time down in the "reasonable domain".
But isn't there any way to create a more real random number generator? I heard about generating numbers using the raw inputs from your mouse, or some other hardware input that seems random. How would I go about that?
P.S. If I srand only once at the beginning of the program (in the main function before I start creating the teams, the results don't seem random at all: number of ABs is always a multiple of 10 as is the number of hits per team; one team also wins almost everything all the time (every time the away team). All this brought me to the idea that I need to srand (a different number of course) before every rand().
P.S. If you need to see the code I will gladly post it on request.