It is currently Sat Dec 16, 2017 5:36 pm

All times are UTC - 7 hours





Post new topic Reply to topic  [ 13 posts ] 
Author Message
PostPosted: Sun Jun 04, 2017 7:06 pm 
Offline

Joined: Mon Jan 30, 2017 5:20 pm
Posts: 303
Location: Colorado USA
Yeah, this seems pretty difficult. How would I generate random numbers? I need between 0 and 255. Any suggestions?


Top
 Profile  
 
PostPosted: Sun Jun 04, 2017 7:17 pm 
Offline
User avatar

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 5899
Location: Canada
http://wiki.nesdev.com/w/index.php/Random_number_generator


Top
 Profile  
 
PostPosted: Sun Jun 04, 2017 7:19 pm 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10166
Location: Rio de Janeiro - Brazil
These are the simplest pseudo-random number generators I'm aware of:

http://codebase64.org/doku.php?id=base: ... 8-bit_prng
http://codebase64.org/doku.php?id=base: ... 6-bit_prng

But we have lots of topics on this forum about this, just search for "PRNG".


Top
 Profile  
 
PostPosted: Sun Jun 04, 2017 7:22 pm 
Offline

Joined: Mon Jan 30, 2017 5:20 pm
Posts: 303
Location: Colorado USA
That would work otherwise, but I'm having a randomly generated level, where we can't base it on stuff like how long the user pressed a button. And I also don't think Tokumaru's would quite work either. The data I'm working with will always have the same result, it's a loop. One register keeps the tile number it will write to the nametable depending on the number, another register has the random number, and another register has how many times it's looped and where in the level it is. So shifting bits would not only corrupt the data, having an EOR would make every single level have the exact same result.


Top
 Profile  
 
PostPosted: Sun Jun 04, 2017 8:13 pm 
Offline
User avatar

Joined: Thu Mar 31, 2016 11:15 am
Posts: 221
Quote:
So shifting bits would not only corrupt the data

:| You have to store X and A in memory before calling the PRNG subroutine, then reload them once it returns.

Quote:
would make every single level have the exact same result.

The levels will only be the same if the PRNG state is exactly the same. To randomize the starting state (i.e. seed the PRNG), you have to rely on other methods such as button presses or startup state.


Top
 Profile  
 
PostPosted: Sun Jun 04, 2017 8:55 pm 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10166
Location: Rio de Janeiro - Brazil
DementedPurple wrote:
That would work otherwise, but I'm having a randomly generated level, where we can't base it on stuff like how long the user pressed a button.

There's no true source of randomness on the NES, seeing as memory tends to be all 0s or all 1s on power up, so the most straightforward seed you can get for a PRNG is one based on user input. Surely your game will have a title screen and maybe other menus before the actual game begins, so just count those frames and use the final result to seed the PRNG.

Quote:
So shifting bits would not only corrupt the data, having an EOR would make every single level have the exact same result.

The 6502 has very few registers, so most kinds logic will require you to swap values between CPU registers and RAM, there's no way around that.


Top
 Profile  
 
PostPosted: Sun Jun 04, 2017 8:59 pm 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 19353
Location: NE Indiana, USA (NTSC)
This topic appears to duplicate Implementing a (pseudo) random number generator.

Many games count frames from power-on until pressing Start and use that to seed the PRNG. There are other ways to generate entropy, such as polling the controller in a DMC IRQ handler (adds roughly 6 bits vs. once per frame) or even exploiting analog behavior of the PPU as in the Pretendo demo.


Top
 Profile  
 
PostPosted: Mon Jun 05, 2017 3:23 am 
Offline
User avatar

Joined: Mon Feb 07, 2011 12:46 pm
Posts: 941
There are a lot of different kind of random number generators. However, for generating entropy, analog effects should probably not be used as the only source, although it may be usable as additional entropy. Initial RAM contents and microphone input also should not be used as the only sources of entropy, but they may be used as additional entropy. The primary sources should probably be time between inputs (e.g. how long before you push start). At least, these are my opinion.

_________________
.


Top
 Profile  
 
PostPosted: Mon Jun 05, 2017 7:55 am 
Offline
User avatar

Joined: Fri May 08, 2015 7:17 pm
Posts: 1869
Location: DIGDUG
There's some truth to this Dilbert Comic. (The RNG outputs 9,9,9,9,9,9). In a true random system, this might actually be random output, which is clearly not what programmers want.

http://dilbert.com/strip/2001-10-25

_________________
nesdoug.com -- blog/tutorial on programming for the NES


Top
 Profile  
 
PostPosted: Mon Jun 05, 2017 8:09 am 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 19353
Location: NE Indiana, USA (NTSC)
Assuming that's 6 random digits (repeat 6 1d10 at Rolz), a programmer wants that result one of every million tries. But 999999 thrice in a row makes it overwhelmingly likely that the RNG is broken.


Top
 Profile  
 
PostPosted: Mon Jun 05, 2017 8:28 am 
Offline
Formerly WheelInventor

Joined: Thu Apr 14, 2016 2:55 am
Posts: 1131
Location: Gothenburg, Sweden
Randomness extracted from user input is great (except if the user input is somehow manipulated before it reaches the program, such as a keyboard buffer).

Anectode on broken RNGs: This is what PHP rand() looks like on windows. Not so random.

_________________
http://www.frankengraphics.com - personal NES blog


Top
 Profile  
 
PostPosted: Mon Jun 05, 2017 10:16 am 
Offline
User avatar

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 5899
Location: Canada
We're getting into the usual round of commentary about PRNGs, but I suspect the OP's confusion is on a more basic level of not understanding the difference between generating a seed for a PRNG and using the PRNG sequence, and possibly at the same time not understanding how registers and memory storage works?


Top
 Profile  
 
PostPosted: Mon Jun 05, 2017 12:18 pm 
Offline

Joined: Wed Nov 30, 2016 4:45 pm
Posts: 93
Location: Southern California
If there's a timer running anywhere in the system, reading its value to use for part of the RNG's operation usually works well since you don't know where it will be in its cycle when the random number is needed.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 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