It is currently Thu Jul 19, 2018 10:18 pm

 All times are UTC - 7 hours

 Page 1 of 1 [ 13 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: How would I generate random numbers?Posted: Sun Jun 04, 2017 7:06 pm

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

Top

 Post subject: Re: How would I generate random numbers?Posted: Sun Jun 04, 2017 7:17 pm

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 6416
Location: Canada
Top

 Post subject: Re: How would I generate random numbers?Posted: Sun Jun 04, 2017 7:19 pm

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10612
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

 Post subject: Re: How would I generate random numbers?Posted: Sun Jun 04, 2017 7:22 pm

Joined: Mon Jan 30, 2017 5:20 pm
Posts: 317
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

 Post subject: Re: How would I generate random numbers?Posted: Sun Jun 04, 2017 8:13 pm

Joined: Thu Mar 31, 2016 11:15 am
Posts: 318
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

 Post subject: Re: How would I generate random numbers?Posted: Sun Jun 04, 2017 8:55 pm

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10612
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

 Post subject: Re: How would I generate random numbers?Posted: Sun Jun 04, 2017 8:59 pm

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

 Post subject: Re: How would I generate random numbers?Posted: Mon Jun 05, 2017 3:23 am

Joined: Mon Feb 07, 2011 12:46 pm
Posts: 991
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

 Post subject: Re: How would I generate random numbers?Posted: Mon Jun 05, 2017 7:55 am

Joined: Fri May 08, 2015 7:17 pm
Posts: 2155
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

 Post subject: Re: How would I generate random numbers?Posted: Mon Jun 05, 2017 8:09 am

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

 Post subject: Re: How would I generate random numbers?Posted: Mon Jun 05, 2017 8:28 am
 Formerly WheelInventor

Joined: Thu Apr 14, 2016 2:55 am
Posts: 1655
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

 Post subject: Re: How would I generate random numbers?Posted: Mon Jun 05, 2017 10:16 am

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 6416
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

 Post subject: Re: How would I generate random numbers?Posted: Mon Jun 05, 2017 12:18 pm

Joined: Wed Nov 30, 2016 4:45 pm
Posts: 110
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

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

 All times are UTC - 7 hours

#### Who is online

Users browsing this forum: No registered users and 1 guest

 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
Powered by phpBB® Forum Software © phpBB Group