Sign in to follow this  

RESOLVED - Random Number problems

This topic is 4305 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by trevaaar
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.


trevaar - I removed <iostream> from dice.cpp since that file does not make an output to the console. When I do this, I receive an error stating 'rand': identifier not found. I removed <time.h> from dice.cpp as you suggested and the program still works correctly. I also replaced <time.h> with <ctime> in main.cpp. The program still works correctly. Are there any advantages to using <ctime> than <time.h>?



Share this post


Link to post
Share on other sites
Standardization. You can't really guarantee anything about what your compiler things "time.h" is in a C++ context. It might be a hacked wrapper around ctime, dumping the contents into the std namespace for you; it might be an entirely different implementation. And it might be something where the compiler sees 'time.h' and applies its crazy monkey logic and decides "oh, never mind this .cpp extension; clearly the user wants me to interpret this as plain C code" and then reports a million errors about keywords like "class" and "new". Of course, it might work just fine on *your* compiler, but there's no guarantee available here.

Share this post


Link to post
Share on other sites
Quote:
Original post by CTEagle
Quote:
Original post by trevaaar
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.


trevaar - I removed <iostream> from dice.cpp since that file does not make an output to the console. When I do this, I receive an error stating 'rand': identifier not found. I removed <time.h> from dice.cpp as you suggested and the program still works correctly. I also replaced <time.h> with <ctime> in main.cpp. The program still works correctly. Are there any advantages to using <ctime> than <time.h>?


The error you're getting about rand is because <iostream> includes <cstdlib>, which is the header that contains rand an srand.

Share this post


Link to post
Share on other sites

This topic is 4305 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this