It is currently Thu Dec 14, 2017 2:00 pm

All times are UTC - 7 hours





Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Random numbers
PostPosted: Mon Aug 15, 2005 3:51 am 
Offline

Joined: Mon Apr 11, 2005 6:04 am
Posts: 20
Location: Kiev, Ukraine
Is it possible to generate random numbers on the NES? I have read that the noise channel plays random frequencys (is it so? english not very good). But can you read the random values and use them in your programm?

_________________
"Wearing of this garment does not enable you to fly."
-- On a child sized Superman costume.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 15, 2005 5:32 am 
Offline
User avatar

Joined: Fri Nov 12, 2004 2:49 pm
Posts: 7314
Location: Chexbres, VD, Switzerland
You can found programm for random numbers and other arithmetic stuff on http://www.6502.org

_________________
Life is complex: it has both real and imaginary components.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 15, 2005 6:44 am 
Offline
User avatar

Joined: Wed Nov 10, 2004 6:47 pm
Posts: 1845
Computers can't really generate true random numbers -- instead they use "pseudo random number generators" which use math formulas which produce numbers which appear to be random. They work by taking an initial value (the 'seed'), running it through a math formula, and outputting the result -- while using that result as the seed for the next generated number. On PCs and other newer systems these formulas are probably done with heavier math operations, but on older systems they were accomplished with simple bitshifting and XOR operations (and possibly with some addition as well).

As Bregalad suggested, check that site and look at the PRNGs. You're probably better off using a premade one than trying to write your own -- as making a good one can be somewhat difficult (it's actually kind of hard to come up with a formula where the numbers don't appear to follow an obvious pattern).


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 15, 2005 7:20 am 
Offline
User avatar

Joined: Mon Sep 27, 2004 8:33 am
Posts: 3715
Location: Central Texas, USA
Here's one I use, based on one from 6502.org:

Code:
; Requires two bytes in memory that don't get modified by anything else.
; They don't need to be next to each other. Set them to reseed the generator.
rand_h = $00
rand_l = $01

; Generate pseudo-random 8-bit value and return in A.
; Preserved: X, Y
random:
      ; See "linear-congruential random number generator" for more.
      ; rand = (rand * 5 + 0x3611) & 0xffff;
      ; return (rand >> 8) & 0xff;
      lda   rand_h      ; multiply by 5
      sta   temp
      lda   rand_l
      asl   a           ; rand = rand * 4 + rand
      rol   temp
      asl   a
      rol   temp
      clc
      adc   rand_l
      pha
      lda   temp
      adc   rand_h
      sta   rand_h
      pla               ; rand = rand + 0x3611
      clc
      adc   #$11
      sta   rand_l
      lda   rand_h
      adc   #$36
      sta   rand_h
      rts               ; return high 8 bits


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 15, 2005 1:37 pm 
Offline
Site Admin
User avatar

Joined: Mon Sep 20, 2004 6:04 am
Posts: 3487
Location: Indianapolis
Of course, all these pseudo-random # generators are totally predictable. But that can be used to your advantage also, like how River Raid on Atari 2600 used fixed seeds to generate it's level data.

To get some actual randomness, you have to get a seed from the only part of the system that you can never predict. And that's the person playing the game. You can increment the seed while waiting for the user on the title screen for example, because you never know exactly what microsecond and frame it will be when they press the start button.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 15, 2005 11:11 pm 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 19345
Location: NE Indiana, USA (NTSC)
Memblers wrote:
because you never know exactly what microsecond and frame it will be when they press the start button.

Oh really? Player loads the NES with Tetris, turns on "slow motion" feature of a third-party controller, which rapidly presses Start, and then turns on the NES. This causes the game to seed the PRNG based on the pre-defined control stream sent by the controller. Because there is only one such control sequence, there will be only one random number seed and thus one possible sequence of pieces. Play by memory.

Even without a slow motion feature, most people can time it to get one of about ten random sequences if you base the seed only on the number of frames elapsed during the title screen.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 15, 2005 11:54 pm 
Offline
Site Admin
User avatar

Joined: Mon Sep 20, 2004 6:04 am
Posts: 3487
Location: Indianapolis
Interesting, I hadn't thought of that before. If the autofire rate is based on the controller's strobe signal (likely would be, I think), then I can definitely see that trick working.

I think this would also work sometimes by just holding the start when you turn the system on. If the game is only checking if the button is pressed, rather than looking for a change in the button state. This is a slight problem with the Game Genie too IIRC. Perhaps they should've done what I did on the Garage Cart's menu and go only after start is released.

tepples wrote:
Even without a slow motion feature, most people can time it to get one of about ten random sequences if you base the seed only on the number of frames elapsed during the title screen.


Yep, which is also why I mentioned microseconds in addition to frames. If it mattered enough, the program could have the idle loop doing it's usual wait for vblank, but also reading the controller and incrementing the seed. That would make reproducing a seed much tougher.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 1:07 am 
Offline
User avatar

Joined: Fri Nov 12, 2004 2:49 pm
Posts: 7314
Location: Chexbres, VD, Switzerland
Thanks, blargg, I had one wich was accurate, but much more complicated, it needed 4 seeds and 4 temp registers.
A good way to test the acuracy of a random number generator is to put the outpout to $4011, then call it again, etc... If you hear a noise wave, you're sure that it is accurate.
Memblers, your trick isn't stupid at all. I think that Dragon Warrior does something like that on it's title screen.

_________________
Life is complex: it has both real and imaginary components.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 2:42 pm 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 19345
Location: NE Indiana, USA (NTSC)
Memblers wrote:
Interesting, I hadn't thought of that before. If the autofire rate is based on the controller's strobe signal (likely would be, I think), then I can definitely see that trick working.

The slow-mo trick won't work on my tetris clone because pressing Start just makes the playfields appear. You need to press A+B to join and then A to start.

Quote:
I think this would also work sometimes by just holding the start when you turn the system on. If the game is only checking if the button is pressed, rather than looking for a change in the button state. This is a slight problem with the Game Genie too IIRC. Perhaps they should've done what I did on the Garage Cart's menu and go only after start is released.

But how much empty space was there in the Game Genie ROM to put this extra safeguard in?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 7 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group