RESOLVED - Random Number problems

Started by
11 comments, last by trevaaar 18 years, 1 month ago
I am just learning to use header files and I made a small console application. The application is a dice roller. Currently I have it coded to roll 2 6 sided dice. I also have the program set to roll those dice 10 times. If I test the code in debug mode, everthing works correctly. I get 10 results between 2 and 12. When I run the program normally, all ten results are identical. I cannot figure out why this is happening. Any thoughts would be greatly appreciated. I copied my code below. Main.cpp # include <iostream> # include <time.h> # include "generator.h" int main() { short loop; short Result; for (loop = 0;loop < 10;loop ++) { Result = Roll(6, 2); std::cout<<Result<<std::endl; } } Dice.cpp # include "generator.h" # include <time.h> # include <iostream> short Roll(short Sides, short NumOfDice) { short Roll; short NumOfRolls; short Temp; Roll = 0; srand (time(NULL)); for (NumOfRolls = 0;NumOfRolls < NumOfDice; NumOfRolls ++) { Temp = rand()% Sides + 1; Roll = (Roll+Temp); } return Roll; } generator.h #pragma once short Roll(short Sides, short NumOfDice); [Edited by - CTEagle on March 3, 2006 12:59:19 PM]
Advertisement
Call srand() once at program startup and remove the call to srand from Roll().

[Edited by - Trap on March 3, 2006 6:45:29 PM]
Thanks Trap, that did the trick. Everything is working correctly.
I dont know understand why that would make a difference. Could you please explain Trap, thank you.
rand() returns a long repeating sequence of numbers. srand() sets the position in this sequence.

If you call srand() every time before calling rand() the return value only depends on the value given to srand().
Ok that makes since, because srand(time(NULL)) is always giving the same result, but if he used srand(GetTickCount()), it would be ok.
Quote:Original post by ordered_disorder
but if he used srand(GetTickCount()), it would be ok.

That would be a bad idea too, since many RNGs simply output the seed as their first random value. It's not how srand is meant to be used.
Quote:Original post by ordered_disorder
I don't understand why that would make a difference.


If you don't seed the RNG you get the same sequence of numbers
over and over. If you seed the RNG with the time inside a function
that will get called multiple times you see the same number repeated
over and over.

I was taught that this last case was due to a time issue. If you
seed the RNG with the time inside of a tight loop it gets seeded
with the same seed over and over producing the same "random" number.

You could probably test this by putting a pause in the middle of the
program and see if the number changes halfway through.

Also note that you only need to include <iostream> in the file that outputs to the console, the C++ standard header for time is <ctime> now rather than <time.h> and that you only have to include that in the code file that uses the time function.
Quote:Original post by Sneftel
Quote:Original post by ordered_disorder
but if he used srand(GetTickCount()), it would be ok.

That would be a bad idea too, since many RNGs simply output the seed as their first random value. It's not how srand is meant to be used.
The commonly implemented Linear Congruential Generator "rand" doesn't do that.
Besides, surely if something is random enough for a seed, it is also random enough for the first raw result, anyway.

I've read that a time_t will be 64 bits on a 64-bit platform, and that the better way to get a seed from this is to generate a hash from it.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement