random number seeding issue
Moderator: Stef
random number seeding issue
Everytime I run a Rom that generates 4 random numbers they are always the same. How do I get the function to seed properly?
-
- Very interested
- Posts: 3131
- Joined: Thu Nov 30, 2006 9:46 pm
- Location: France - Sevres
- Contact:
Re: random number seeding issue
Did you try the random() method ? On emulator the seeder can contains the same value but on real hardware you should obtain real random sequence.
Re: random number seeding issue
Yes I'm using the random() method! So on emulators the seeder contains the same values? lolStef wrote:Did you try the random() method ? On emulator the seeder can contains the same value but on real hardware you should obtain real random sequence.
-
- Very interested
- Posts: 3131
- Joined: Thu Nov 30, 2006 9:46 pm
- Location: France - Sevres
- Contact:
Re: random number seeding issue
Yeah because it relies on HV counter value at cold start, which is often the same value on emulator but not on real hardware
Re: random number seeding issue
It could be useful to determine whether you're running from real hw or an emu ?
Re: random number seeding issue
always on emulator at the moment!
Re: random number seeding issue
Is there a way around this?
Re: random number seeding issue
This is common to all consoles and embedded environments with no RNG. You have to get entropy from the only place available, the user.
For most titles, this means calling random() once per frame on the title screen. The time before the user presses a key varies, so will your randomness.
For most titles, this means calling random() once per frame on the title screen. The time before the user presses a key varies, so will your randomness.
-
- Very interested
- Posts: 3131
- Joined: Thu Nov 30, 2006 9:46 pm
- Location: France - Sevres
- Contact:
Re: random number seeding issue
I've changed the random() method in SGDK to generate the same random numbers sequence for a given seeder. If you want different value at each start then you should initialize the random seed (using setRandomSeed(..) method) with a random value at start (using HV counter on real hardware or first user controller event time).
Re: random number seeding issue
So does that basically mean my software will only work properly on proper hardware? :/
-
- Very interested
- Posts: 3131
- Joined: Thu Nov 30, 2006 9:46 pm
- Location: France - Sevres
- Contact:
Re: random number seeding issue
I edited my previous reply, but generally yeah emulators won't produce reliable random() as o real hardware if you use the HV counter value to initialize the random seeder.
Re: random number seeding issue
You may introduce button presses and frame counter to the formula, that should work on emus. Also using the full register, H and V counter, should make a difference.
HELP. Spanish TVs are brain washing people to be hostile to me.
Re: random number seeding issue
okay I almost understand So do I call setRandomSeed after VDP_waitVSync? and if so what do I need to put into the Random Seed? What functions can I call to get the event time?
Re: random number seeding issue
I wanted to check my method for doing this was correct!
Code: Select all
u16 randomNumberGenerator(u16 startInt, u16 finishInt) {
if (finishInt != 0)
{
u16 x = ( random() % (finishInt - startInt) ) + startInt;
return x;
}
else
{
return 0;
}
}
Re: random number seeding issue
Bringing in new knowledge:
A brilliant idea that konami games did is to use idle time to run the random number generator. When you are waiting for the vblank exception on the main loop, instead of just waiting, run the generator.
Since the idle time changes depending on everything else you got the better change for random numbers.
While your code is fine, if you can is better to use advantages of binary/hexadecimal numbers.
A brilliant idea that konami games did is to use idle time to run the random number generator. When you are waiting for the vblank exception on the main loop, instead of just waiting, run the generator.
Since the idle time changes depending on everything else you got the better change for random numbers.
While your code is fine, if you can is better to use advantages of binary/hexadecimal numbers.
Code: Select all
u16 rNumber = random();
switch(rNumber & 3)
{
case 0: move up; break;
case 1: move down; break;
case 2: move left; break;
case 3: move right; break;
}
if(rNumber & 4)
{
HitWithSword();
}
if(rNumber & 8)
{
Jump();
}
if(rNumber & 16)
{
Retreat();
}
HELP. Spanish TVs are brain washing people to be hostile to me.