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

Started by
11 comments, last by Wooh 12 years, 8 months ago
[font="Consolas"][font="Consolas"]

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 http://www.johndcook...ndom.html#start.

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]static[/font][/font][/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[/font][/font][/font]

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

std::tr1::uniform_int_distribution <

[/font]

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

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

[/font]

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

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

[font="Consolas"][font="Consolas"] 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.

[/font][/font]
Advertisement
You need to pass the seed to the seed function, or you will get a default seed. Pass, say, the current time to it, or something that varies every time you run the program. The seed is just an unsigned integer.
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():

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;
}
}
uniform_int_distribution is called uniform_int in tr1. uniform_int_distribution is what it's called in C++0x.
I have used [font="Consolas"][font="Consolas"]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"] [font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"]//Time management bits.[/font][/font][/font] [font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]__int64[/font][/font][/font][font="Consolas"][font="Consolas"] int64ClockFrequency, int64PreviousTime, int64CurrentTime;[/font][/font] [font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]float[/font][/font][/font][font="Consolas"][font="Consolas"] fltNormalisedSecsPerCycle, fltDeltaTime;[/font][/font]

[font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"]//Mersenne Twister random engine for generation of random value[/font][/font][/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

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

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


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

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

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

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

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

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

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

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

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

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

[/font]

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

[/font][/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.[/font][/font][/font][/font]
Also I switched to uniform_int, thankyou for pointing that out for me Wooh
Why are you reseeding?

Why are you reseeding?



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.[font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]unsigned[/font][/font][/font][font="Consolas"] [/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]long[/font][/font][/font][font="Consolas"][font="Consolas"] ulong = [/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]unsigned[/font][/font][/font][font="Consolas"] [/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]long[/font][/font][/font][font="Consolas"][font="Consolas"](fltDeltaTime * 1000000000);[/font][/font]

[font="Consolas"][font="Consolas"] 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?

[/font][/font]
[font="Consolas"][font="Consolas"]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());

[/font][/font]

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.

Why does that require you to reseed?

This topic is closed to new replies.

Advertisement