random number seeding issue

SGDK only sub forum

Moderator: Stef

Post Reply
matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

random number seeding issue

Post by matteus »

Everytime I run a Rom that generates 4 random numbers they are always the same. How do I get the function to seed properly?
Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: random number seeding issue

Post by Stef »

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.
matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

Re: random number seeding issue

Post by matteus »

Stef 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.
Yes I'm using the random() method! So on emulators the seeder contains the same values? :D lol
Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: random number seeding issue

Post by Stef »

Yeah because it relies on HV counter value at cold start, which is often the same value on emulator but not on real hardware ;-)
tryphon
Very interested
Posts: 316
Joined: Sat Aug 17, 2013 9:38 pm
Location: France

Re: random number seeding issue

Post by tryphon »

It could be useful to determine whether you're running from real hw or an emu ?
matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

Re: random number seeding issue

Post by matteus »

always on emulator at the moment! :)
matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

Re: random number seeding issue

Post by matteus »

Is there a way around this? :D
cero
Very interested
Posts: 340
Joined: Mon Nov 30, 2015 1:55 pm

Re: random number seeding issue

Post by cero »

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.
Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: random number seeding issue

Post by Stef »

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).
matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

Re: random number seeding issue

Post by matteus »

So does that basically mean my software will only work properly on proper hardware? :/
Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: random number seeding issue

Post by Stef »

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.
Miquel
Very interested
Posts: 514
Joined: Sat Jul 30, 2016 12:33 am

Re: random number seeding issue

Post by Miquel »

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.
matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

Re: random number seeding issue

Post by matteus »

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?
matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

Re: random number seeding issue

Post by matteus »

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;
    }
}
Miquel
Very interested
Posts: 514
Joined: Sat Jul 30, 2016 12:33 am

Re: random number seeding issue

Post by Miquel »

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.

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.
Post Reply