It is currently Wed Jul 18, 2018 11:43 am

 All times are UTC - 7 hours

 Page 1 of 1 [ 9 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Random numbersPosted: Mon Aug 15, 2005 3:51 am

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

 Post subject: Posted: Mon Aug 15, 2005 5:32 am

Joined: Fri Nov 12, 2004 2:49 pm
Posts: 7443
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

 Post subject: Posted: Mon Aug 15, 2005 6:44 am

Joined: Wed Nov 10, 2004 6:47 pm
Posts: 1849
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

 Post subject: Posted: Mon Aug 15, 2005 7:20 am

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
pha
lda   temp
sta   rand_h
pla               ; rand = rand + 0x3611
clc
sta   rand_l
lda   rand_h
sta   rand_h
rts               ; return high 8 bits

Top

 Post subject: Posted: Mon Aug 15, 2005 1:37 pm

Joined: Mon Sep 20, 2004 6:04 am
Posts: 3528
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

 Post subject: Posted: Mon Aug 15, 2005 11:11 pm

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 20261
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

 Post subject: Posted: Mon Aug 15, 2005 11:54 pm

Joined: Mon Sep 20, 2004 6:04 am
Posts: 3528
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

 Post subject: Posted: Tue Aug 16, 2005 1:07 am

Joined: Fri Nov 12, 2004 2:49 pm
Posts: 7443
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

 Post subject: Posted: Tue Aug 16, 2005 2:42 pm

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 20261
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

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 9 posts ]

 All times are UTC - 7 hours

#### Who is online

Users browsing this forum: gauauu and 6 guests

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

Search for:
 Jump to:  Select a forum ------------------ NES / Famicom    NESdev    NESemdev    NES Graphics    NES Music    Homebrew Projects       2018 NESdev Competition       2017 NESdev Competition       2016 NESdev Competition       2014 NESdev Competition       2011 NESdev Competition    Newbie Help Center    NES Hardware and Flash Equipment       Reproduction    NESdev International       FCdev       NESdev China       NESdev Middle East Other    General Stuff    Membler Industries    Other Retro Dev       SNESdev       GBDev    Test Forum Site Issues    phpBB Issues    Web Issues    nesdevWiki