vendredi 14 août 2015

C++ Random number generator: How to make numbers random upon every execution

I don't think this is a duplicate because I've tried other SO answers and they don't work for me. When I run a program (seeded with time) I get a bunch of random numbers. Then a few seconds later I run the program again and get a similar bunch of random numbers.

srand(time(0));
for(int i=0; i<10; i++) {
    cout<<rand()<<"\n";
}

8603 55 3146 26251 14799 16799 28230 314 3602 9504

8639 19984 3044 28803 29955 27225 29699 882 21389 7411

As you can see the numbers are different, but really similar. The reason this won't work for me is because I'm converting it into a float between 0 and 1 (dividing by RAND_MAX). Time in seconds is ticking extremely slowly when I do that. On one run I could have 0.09245... and next I could have 0.0925... . I'm using these probabilities to make a blackjack AI, and I don't want the same patterns in betting to show up in subsequent executions.

I've looked many around a lot and tried to put the seed inside the loop, but that just printed the same number 10 times. Not sure how to make the numbers appear completely random from execution time. Thanks for your ideas




Aucun commentaire:

Enregistrer un commentaire