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 » Tue Nov 24, 2015 9:20 am

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 » Tue Nov 24, 2015 9:33 am

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 » Tue Nov 24, 2015 5:36 pm

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 » Tue Nov 24, 2015 5:44 pm

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 » Tue Nov 24, 2015 5:51 pm

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 » Tue Nov 24, 2015 6:22 pm

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 » Tue Sep 13, 2016 8:36 pm

Is there a way around this? :D

cero
Very interested
Posts: 338
Joined: Mon Nov 30, 2015 1:55 pm

Re: random number seeding issue

Post by cero » Wed Sep 14, 2016 7:52 am

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 » Wed Sep 14, 2016 1:43 pm

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 » Fri Sep 16, 2016 11:06 am

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 » Fri Sep 16, 2016 11:36 am

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 » Sat Sep 17, 2016 12:10 am

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 » Mon Sep 19, 2016 5:14 pm

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 » Sun Jan 14, 2018 11:00 am

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 » Sun Jan 14, 2018 12:54 pm

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