Random Number Generator - No Extra Cog
ryfitzger227
Posts: 99
Hey guys!
I need help with something. I'm in need of some type of random generator that gives true random values. I know there's some stuff in the Object Exchange, but not any that doesn't require an extra cog - something I don't have in this project. I need something that just gets a random value between 1 or 2, but I can also deal with 0-9 (in that case odd would equal 1 and even would equal 2 - still a 50% chance like 1-2). What I was thinking was to take the last character in cnt, but everything I tried wouldn't work. Any ideas on how I could do that, or just get a random number without an extra cog?
Thanks.
- Ryan
I need help with something. I'm in need of some type of random generator that gives true random values. I know there's some stuff in the Object Exchange, but not any that doesn't require an extra cog - something I don't have in this project. I need something that just gets a random value between 1 or 2, but I can also deal with 0-9 (in that case odd would equal 1 and even would equal 2 - still a 50% chance like 1-2). What I was thinking was to take the last character in cnt, but everything I tried wouldn't work. Any ideas on how I could do that, or just get a random number without an extra cog?
Thanks.
- Ryan
Comments
http://forums.parallax.com/showthread.php/136975-JKISS32-High-quality-PRNG-with-RealRandom-seed-save-a-COG.
Basically the idea is to use a simple but high quality pseudo random number generator, which does not need a COG, and seed it from the RealRandom object, which does use a COG. Once the seeding is done the RealRandom COG is shut down and can be reused.
Depends what you mean by "True Random". You could of course do this with the Spin built in random operator but it is not a very good generator. The pseudo random number generator above will provide you with seeming randomness that does not repeat itself for thousands of years and gives a different sequence every time you reset. It does very well when tested with all the statistical tests of randomness I could find.
If you only need two values out, heads or tails, just use the first bit of each number generated. Or shift 32 bits out of each one.
EDIT: fixed a bug on Feb 13 Now, this has in NO WAY been tested, other than watching a histogram for a while. I leave it to the PRNG gurus to figure out how to test the randomness of this code. Also note that this will step on CTRA for the calling cog.
Jonathan
Andy
The Spin pseudo random number generator has a very short cycle time and fails all statistical tests for randomness.
According to the manual :
Upon power-up/reset, the System Counter starts with an arbitrary value and counts upwards
from there, incrementing with every System Clock cycle.
Not sure what that means. Sounds like it is not set to any fixed value like zero on reset which might be OK but presumably from power up it may always find itself in the same starting position (might vary from chip to chip). If that is the case using it as a seed is hardly random at all.
This kind of thing used to be the flaw in on-line poker sites back in the day making them easily abusable.
Things like JKISS32 and a real random seed are order of magnitude better. But even they are not recommended for use in cryptographic applications where security is a prime concern. However cryptographically safe random number generators are a lot more complex and slower.
Looks like lonesock has just made my JKISS32 scheme obsolete. Except there are times when you do actually want a pseudo random number generator. When comparing simulation models that driven by random input for example. And of course there is that stepping on the Props hardware to consider.
P.S. Of course even statistical tests of randomness can't produce absolute yes/no answers to the question "Is this random".
Of course, if the Spin version of RealRandom does work, or can be made to work, it might be a nice alternative to using a cog every once in a while in your JKISS32 object.
Jonathan
Jonathan
Cool.
Sounds like I have to run your random output through the Die Hard and other tests of randomness.
I wonder which is quicker, JKISS32 or just going straight to your RealRandom.
EDIT: RealRandomSpin takes about 13 [ms] per 32-bit long! So, at 77 random longs per second, yeah, definitely use it only as a seed for a decent PRNG! [8^)
Jonathan
The worst total period I have observed < 320 clocks, so you can have about 250k random numbers a second. A new state variable is swapped out every 60 periods or so, so all 5 will have been replaced about 1000 times a second or so (at 80MHz). Meanwhile the JKISS32 code is mixing it's heart out for every period.
All in all, barring unfortunate bugs of mine, it should be a nice way to get lots of really random numbers quickly. It really needs more testing though.
EDIT: I removed the code...the new version is a few posts down.
Jonathan
1 - I used a SAR when I should have used a SHR (Heater's code had it done correctly, as the original C code used unsigned integers)
2 - The carry variable C should not be 0/1, but rather 0/-1 (Heater, I think this is an issue in your Spin code as well? Not sure how much it matters, of course.)
I will update the object in my previous post.
EDIT: curses, I was wrong on the comparison going to -1...hanging head in shame and updating code once again!
Jonathan
No need for shame. I hit those exact same signedness problems.
Here is a funny story:
Professor George Marsaglia, who invented the random number generating technique that JKISS32 is based on, published a CD of random numbers back in 1995.
Problem is that the published CD fails the "Die Hard" statistical tests of randomness.
What happened?
Turns out that when he copied the binary file under MSDOS it was copied as a text file instead of a binary file. So every OxOD got replaced with a 0x0A0x0D. Or whatever it MSDOS does when it sees an end of line in a text file.
As a result there are statistically to many 0x0d0x0a or 0x0a0x0d sequences in the file on that CD ROM.
Another great man screwed over by MicroSoft.
I did find one more bug (this one _was_ cause for shame...copy-n-paste error [8^), sped up the code, and used 32 real random bits when updating the X/Y/Z/W state variables, but only 1 real random bit when updating the C state variable. I also added in the RealRandomSpin function, just to have all this in one place. I'm posting it here, but I don't think I will do much more to this code unless someone needs something else?
Jonathan