Seeding the Random Number Generator

Topics related to the API, programming discussions & questions, coding tips, bugs, etc. should go here.
User avatar
D3thAdd3r
Posts: 2424
Joined: Wed Apr 29, 2009 10:00 am
Location: Minneapolis, United States

Re: Seeding the Random Number Generator

Post by D3thAdd3r » Sun Sep 07, 2014 3:19 am

Well I started to tinker with it and of course I got stuck on the absolute basics...I can't actually get the watchdog to throw an ISR. This was my basic idea on how it would work but I have been playing around with different methods and still haven't got that ISR to fire. Haven't tried debugging it yet of course...

Code: Select all

//Watchdog timeout ISR
ISR(WDT_vect)
{
TriggerFx(1,255,1);
	if(vram[2] < 7){
		vram[0] <<= 1;
		vram[0] |= (TCNT1&1);
	}else{
		vram[1] <<= 1;
		vram[1] |= (TCNT1&1);
	}
	vram[2]++;
}

void wdt_init(void)
{

	cli();
	MCUSR = 0; // Reset the status register of the MCU
	wdt_disable (); // Disables the watchdog
	WDTCSR = 0;
	//set watchdog register
	WDTCSR |= ((1 << WDCE) | (1 << WDE));
	//interrupt with 15ms timeout
	WDTCSR = ((1 << WDIE) | (0 << WDP2) | (0 << WDP1)); 


	while(vram[2] < 16){
	//by the time we escape this, we should have 16 bits of entropy in vram[0]:vram[1]
	}
	sei();

    MCUSR = 0;
    wdt_disable();
    return;
}

CunningFellow
Posts: 1196
Joined: Mon Feb 11, 2013 8:08 am
Location: Brisbane, Australia

Re: Seeding the Random Number Generator

Post by CunningFellow » Sun Sep 07, 2014 6:10 am

So elsewhere in your code you have enabled the watchdog interrupt (WDIE in WDTCSR) and set the prescaler ?

User avatar
D3thAdd3r
Posts: 2424
Joined: Wed Apr 29, 2009 10:00 am
Location: Minneapolis, United States

Re: Seeding the Random Number Generator

Post by D3thAdd3r » Sun Sep 07, 2014 4:08 pm

Hmm what do you mean exactly? I wouldn't doubt I was missing something obvious here, but shouldn't this code setup the watchdog and the prescaler:

Code: Select all

   //set watchdog register
   WDTCSR |= ((1 << WDCE) | (1 << WDE));
   //interrupt with 15ms timeout
   WDTCSR = ((1 << WDIE) | (0 << WDP2) | (0 << WDP1)); 
My intention is, if the user sets up a compile switch, to basically make 16 random bits once when the Uzebox starts up then disable watchdog forever. Then the user can take vram[0]..vram[1](before they trash it) as a seed for their LFSR so it is a random place to start in a deterministic pattern. It should take about 15ms*16=240 ms so I don't intend for it to happen more than once.

User avatar
uze6666
Site Admin
Posts: 4487
Joined: Tue Aug 12, 2008 9:13 pm
Location: Montreal, Canada
Contact:

Re: Seeding the Random Number Generator

Post by uze6666 » Mon Sep 08, 2014 12:15 am

Got it to work! :D And yes, it really seems truly random. Here's a quick demo that shows it. Only runs on the real hardware since uzem doesn't implement the timer watchdog interrupt. Seems to work better if I run the code before the kernel's initialization stuff in order to use the full range of timer 1 (normally it rolls over at 1820). That also means it not practical to generate numbers on the fly during game play or the screen will flicker. But it's no big deal since all we want is generate a unique seed.

I'll wrap up the code and commit it shortly.
Attachments
random.hex
(33.45 KiB) Downloaded 120 times

User avatar
uze6666
Site Admin
Posts: 4487
Joined: Tue Aug 12, 2008 9:13 pm
Location: Montreal, Canada
Contact:

Re: Seeding the Random Number Generator

Post by uze6666 » Mon Sep 08, 2014 1:12 am

Code commited to the trunk. Use this makefile switch to enable support:

Code: Select all

-DTRUE_RANDOM_GEN=1
This will generate the seed when the kernel initializes. It's pretty fast, you won't see any differences. There's actually a library already made for Arduino it seems, but it's way overkill with all kind of weird bit shifting and XORs. This version is as simple as possible yet seemingly random enough. In your code you can retrieve the seed using the new GetRandomSeed() function. For instance, you can seed the C library fucntion with:

Code: Select all

srand(GetRandomSeed());
Let me know how it works out for you guys!
Attachments
random2.hex
(33.52 KiB) Downloaded 122 times

User avatar
D3thAdd3r
Posts: 2424
Joined: Wed Apr 29, 2009 10:00 am
Location: Minneapolis, United States

Re: Seeding the Random Number Generator

Post by D3thAdd3r » Mon Sep 08, 2014 2:43 am

Nicely done Alec 8-)

User avatar
PitfallJones
Posts: 39
Joined: Mon Aug 16, 2010 12:01 am

Re: Seeding the Random Number Generator

Post by PitfallJones » Tue Sep 09, 2014 3:03 pm

Thanks for that - quite excellent.
In the end I used the wait for keypress method in Buddha but this will be most useful in future.
- PJ

User avatar
D3thAdd3r
Posts: 2424
Joined: Wed Apr 29, 2009 10:00 am
Location: Minneapolis, United States

Re: Seeding the Random Number Generator

Post by D3thAdd3r » Mon Apr 27, 2015 6:18 am

This is a actually a very interesting feature since I never saw any classic game console capable of having an attract mode that wasn't always the same order when powered up. I left it out of my last couple games because it locks the emulator. I have no clue how to implement timers stuff in the emulator but I would like to use this feature(I don't think anyone has yet). Do we have any idea how much work it would be to update the emulator with this?

Alec I recall some time ago you were kind enough to implement the request for the function IsRunningInEmulator(). I know it's use has an automatic shroud of nasty hack around it. There are still some good features that aren't emulated and with the advent of the ram likely to be more for a time. Is there some clean way for a program to detect what features are emulated so they will work on the real hardware, not run in the emulator, and later work in an updated emulator version without recompilation? I would only think it's valid for things like the true random numbers or SD writing until they get added, but when they aren't emulated they are kind of unusable in general if they hang the game(but aren't necessary for operation).

User avatar
uze6666
Site Admin
Posts: 4487
Joined: Tue Aug 12, 2008 9:13 pm
Location: Montreal, Canada
Contact:

Re: Seeding the Random Number Generator

Post by uze6666 » Mon Apr 27, 2015 9:16 pm

I suspect it's 10-15 hours of work for sure but it should not be too hard. I'll have a look tonight in between two 3D prints...

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests