Sign in to follow this  
EnlightenedOne

TR1 <Random> I can't change the seed.

Recommended Posts

[font="Consolas"][size="2"][font="Consolas"][size="2"]

I implore someone to explain to me why <random> TR1 seed function produces the same result consistently, giving the same set of random numbers everytime I call it. Its driving me cocobananas, this claims to explain it, but it has what barely qualifies as a sentence on seeding [url="http://www.johndcook.com/cpp_TR1_random.html#start"]http://www.johndcook...ndom.html#start[/url].

[/size][/font][/size][/font][size="2"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]static[/color][/font][/color][/font][/color][/font][font="Consolas"][font="Consolas"] std::tr1::mt19937 mtRandGenerator; [/font][/font][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"]//A core engine class[/color][/font][/color][/font][/color][/font][/size]

[font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"] [/font][/color][/font][/color][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] mtRandGenerator.seed();

std::tr1::uniform_int_distribution <

[/size][/font]

[/size][/font][size="2"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]int[/color][/font][/color][/font][/color][/font][font="Consolas"][font="Consolas"]> unifintGenWidth(0, intFSWidth);[/font][/font][/size]

[font="Consolas"][size="2"][font="Consolas"][size="2"] std::tr1::uniform_int_distribution <

[/size][/font]

[/size][/font][size="2"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]int[/color][/font][/color][/font][/color][/font][font="Consolas"][font="Consolas"]> unifintGenHeight(0, intFSHeight);

//I added these when desperate.[/font][/font][/size]

[font="Consolas"][size="2"][font="Consolas"][size="2"] unifintGenWidth.reset();

unifintGenHeight.reset();


Also this forum keeps crashing IE9 whenever I try to add a code caption, so I am simply unable to, im not just being lazy. I can't find a single simple example of this code being (more than once) to produce varying results. Can anyone show me the method to cause it to randomly seed my random numbers? Theres some instant rating points to anyone who can.

[/size][/font][/size][/font]

Share this post


Link to post
Share on other sites
Calling seed() with no arguments uses the same default seed every time. If you want a different seed you need to use different arguments. Some examples of calling seed():
[code]
int main(int, char **) {
{
std::tr1::mt19937 mtRandGenerator;
mtRandGenerator.seed(0);
std::cout << mtRandGenerator() << std::endl;
} {
std::tr1::mt19937 mtRandGenerator;
mtRandGenerator.seed(1);
std::cout << mtRandGenerator() << std::endl;
} {
std::tr1::mt19937 mtRandGenerator;
mtRandGenerator.seed(time(0));
std::cout << mtRandGenerator() << std::endl;
}
}
[/code]

Share this post


Link to post
Share on other sites
I have used [font="Consolas"][size="2"][font="Consolas"][size="2"]time(0) as my seed it is working but I want the seeding to alter faster than once per second, I tried to plug the delta time below multiplied to microseconds converted to an integer but it kept hanging and im not sure why, this is how I was getting delta time.
[font="Consolas"][size="2"] [font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][size="2"][color="#008000"]//Time management bits.[/color][/size][/font][/color][/font][/color][/font] [size="2"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]__int64[/color][/font][/color][/font][/color][/font][font="Consolas"][font="Consolas"] int64ClockFrequency, int64PreviousTime, int64CurrentTime;[/font][/font][/size] [size="2"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]float[/color][/font][/color][/font][/color][/font][font="Consolas"][font="Consolas"] fltNormalisedSecsPerCycle, fltDeltaTime;[/font][/font][/size]

[font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][size="2"][color="#008000"]//Mersenne Twister random engine for generation of random value[/color][/size][/font][/color][/font][/color][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]



std::tr1::mt19937 mtRandGenerator;[/size][/font][/size][/font][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][size="2"][color="#008000"]//A core engine class

[/color][/size][/font][/color][/font][/color][/font][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][size="2"][color="#008000"]//Prepare timing mechanism preuse[/color][/size][/font][/color][/font][/color][/font]

[font="Consolas"][size="2"][font="Consolas"][size="2"]QueryPerformanceCounter((LARGE_INTEGER*)&int64PreviousTime);


//Game loop around the code below[/size][/font][/size][/font][/size][/font][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][size="2"][color="#008000"]//Time gets handled before anything[/color][/size][/font][/color][/font][/color][/font][font="Consolas"][size="2"] [/size][/font][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][size="2"][color="#008000"]//Get the current time.[/color][/size][/font][/color][/font][/color][/font]

[font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"] [/font][/color][/font][/color][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]QueryPerformanceCounter((LARGE_INTEGER*)&int64CurrentTime);[/size][/font]

[/size][/font][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][size="2"][color="#008000"]//The clock frequency can dynamically change in new processors so we get it again.[/color][/size][/font][/color][/font][/color][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]

QueryPerformanceFrequency((LARGE_INTEGER*)&int64ClockFrequency);[/size][/font][/size][/font][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][size="2"][color="#008000"]//Normalise the frequency to a factor of a second.[/color][/size][/font][/color][/font][/color][/font]

[font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"] [/font][/color][/font][/color][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]fltNormalisedSecsPerCycle = 1.0f / (

[/size][/font][/size][/font][size="2"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]float[/color][/font][/color][/font][/color][/font][font="Consolas"][font="Consolas"])int64ClockFrequency;[/font][/font][/size]

[font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][size="2"][color="#008000"]//Get the delta from the current time against the previous multiplied by a normalised version of the[/color][/size][/font][/color][/font][/color][/font] [font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][size="2"][color="#008000"]//clock frequency.[/color][/size][/font][/color][/font][/color][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]

fltDeltaTime = (int64CurrentTime - int64PreviousTime) * fltNormalisedSecsPerCycle[/size][/font]

/[/size][/font][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][size="2"][color="#008000"]/Doesn't matter when this gets done as we handle it next loop.[/color][/size][/font][/color][/font][/color][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]

int64PreviousTime = int64CurrentTime;
[font="Consolas"][size="2"][font="Consolas"][size="2"][font="Consolas"][size="2"][font="Consolas"][size="2"] mtRandGenerator.seed(

[/size][/font]

[/size][/font][size="2"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]unsigned[/color][/font][/color][/font][/color][/font] [font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]long[/color][/font][/color][/font][/color][/font][font="Consolas"][font="Consolas"](fltDeltaTime * 1000000));[/font][/font][/size]

[/size][/font][/size][/font]
Thats seriously as best as I could format that, why is the forum so skrewed up? probably an IE thing. Anyway it would hang on the seed function with no source available... I was hoping it would get its own random seed if you called seed blindly, but thank you for confirming my fault.[/size][/font][/size][/font][/size][/font][/size][/font]

Share this post


Link to post
Share on other sites
[quote name='SiCrane' timestamp='1313423596' post='4849429']
Why are you reseeding?
[/quote]


I wish to display a set of images randomly on the screen every time the reset button or change image library button is hit I want the images to appear in randomised locations over the screen. While I could just use rand I got heavily invested in figuring out <random> and I am nothing if not stubborn.[size="2"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]unsigned[/color][/font][/color][/font][/color][/font][font="Consolas"] [/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]long[/color][/font][/color][/font][/color][/font][font="Consolas"][font="Consolas"] ulong = [/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]unsigned[/color][/font][/color][/font][/color][/font][font="Consolas"] [/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]long[/color][/font][/color][/font][/color][/font][font="Consolas"][font="Consolas"](fltDeltaTime * 1000000000);[/font][/font][/size]

[font="Consolas"][size="2"][font="Consolas"][size="2"] mtRandGenerator.seed(ulong);

It turns out my value is hitting zero even at roughly nanoseconds. Unfortunately to use delta time as a seed I have to seed from within the loop which is randomly generating an image position, which I know is unnecessary as the seed need only change once per reset. I need a means of getting a random value at a higher resolution than a second so I can seed my images positions faster than you can see them being seeded, so millisecond timing would do it. Am I forgetting an obvious clock I can call?

[/size][/font][/size][/font]

Share this post


Link to post
Share on other sites
[font="Consolas"][size="2"][font="Consolas"][size="2"]Using get tick count to solve the issue and seed outside the loop away from delta time, now the only worry is if I am at risk of having tick count exceed the value of an unsigned int?

mtRandGenerator.seed(GetTickCount());

[/size][/font][/size][/font]

Share this post


Link to post
Share on other sites
[quote name='EnlightenedOne' timestamp='1313424074' post='4849434']
I wish to display a set of images randomly on the screen every time the reset button or change image library button is hit I want the images to appear in randomised locations over the screen.
[/quote]
Why does that require you to reseed?

Share this post


Link to post
Share on other sites
[quote name='SiCrane' timestamp='1313425060' post='4849442']
[quote name='EnlightenedOne' timestamp='1313424074' post='4849434']
I wish to display a set of images randomly on the screen every time the reset button or change image library button is hit I want the images to appear in randomised locations over the screen.
[/quote]
Why does that require you to reseed?
[/quote]

Because even with the randomising engine set as static the images positions would be set in order upon the function being recalled, this happened with no call to seed in the function causing the images to repeat for any reason I could determine. Unless the seed is changed the images positions would stubbornly not change. Even if I could of deduced why the images happened to repeat as the function was recalled the program starting on the same seed everytime would have been a problem as the image positions would be the same each time it was run.

Share this post


Link to post
Share on other sites

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