Button press as random seed generator

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems. See the NESdev wiki for more information.

Moderator: Moderators

Post Reply
User avatar
rainwarrior
Posts: 8759
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Button press as random seed generator

Post by rainwarrior »

Here's a little test using the length of time you hold down the start button as a random seed. This could be suitable for a title screen where you press start to begin the game.

http://rainwarrior.ca/projects/nes/buttonlen.zip

I just enter a polling loop when start is held, incrementing it each time. When you release, the 8-bit loop counter is displayed on the screen. ca65 source is included if you want more details.

Actually works pretty well. On emulators I don't think you really get sub-frame timing on the input (I find the number 47 comes up a lot on FCEUX), but on the NES it seems completely random, and even on emulators it's not bad at all. Dwedit mentioned a possible need to debounce when doing this, but I don't seem to have any problem with that on my NES.


Fun fact: the GBA port of Final Fantasy IV seeds its random number generator by number of seconds spent on the title screen. I abused this at one point to finally get a pink tail. (My avatar is related.)
User avatar
Bregalad
Posts: 8115
Joined: Fri Nov 12, 2004 2:49 pm
Location: Divonne-les-bains, France

Re: Button press as random seed generator

Post by Bregalad »

Fun fact: the GBA port of Final Fantasy IV seeds its random number generator by number of seconds spent on the title screen. I abused this at one point to finally get a pink tail. (My avatar is related.)
Honnestly this is quite the natural way to do it.
I just call the random routine in the VBlank (and seed at '1' at reset), and by the time the user will start a new game, the time spent on the title screen is random therefore the RNG is actually random as well.
User avatar
rainwarrior
Posts: 8759
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Button press as random seed generator

Post by rainwarrior »

Counting frames is pretty normal, counting seconds not so much (and very easy to abuse!).
User avatar
Bregalad
Posts: 8115
Joined: Fri Nov 12, 2004 2:49 pm
Location: Divonne-les-bains, France

Re: Button press as random seed generator

Post by Bregalad »

Well I think it's no secret that people behind GBA's ports of Final Fantasy games were a bunch of incompetents.
zzo38
Posts: 1096
Joined: Mon Feb 07, 2011 12:46 pm

Re: Button press as random seed generator

Post by zzo38 »

You can also use microphone and initial contents of RAM, but they should not be used by themself because they might not always be available or working; but I suppose it could be used in combination with button press timing.
(Free Hero Mesh - FOSS puzzle game engine)
Post Reply