I've invented a new pseudo-random number generator
Patrick M (2888) 126 posts |
Hello, It’s been a while since I posted here. I’m hoping to get my Raspberry Pi up and running again so I can do more BBC BASIC programming in RISC OS again soon. Lately, I’ve been studying pseudo-random number generation. I came up with something that I’d like to share with the forum. I’ve developed a technique called “nutting”. “Nutting” is to take the result of a multiplication, fmod 1.0. After experimenting with the “dieharder” and “practrand” test suite software for a few months, I’ve come up with this function: (EDIT: Sorry, the forum software messes up the code by replacing ascii characters with unicode punctuation. Here’s a link to a text file on my webserver if you want to try running the code: http://dusthillresident.ddns.net/folder/supermeganutter_riscos.txt )
REM SuperMegaNutter, version “b7”. DEF FNSuperMegaNutter_b7 The design uses two state variables, nn1 and nn2, which increment and decrement at approximate rates per iteration (made approximate by adding in the results of the nutting operations), which wrap around when the variables get too small or too large. (The wrapping around event is known as the “sideways nutting action”.) In my testing, this version was able to pass 32TB of testing with PractRand when generating 32bit integers, and 32bit integers with the bit order flipped around. This has been really interesting to explore, I’d like to hear any thoughts anyone has about this. Also, in a general sense, I’d be interested to know more about what sorts of PRNG libraries and functions are available on RISC OS. Thank you, |
Paul Sprangers (346) 525 posts |
It may be the bottle of wine, or my unfamiliarity with PractRand, or my ignorance of how |
Patrick M (2888) 126 posts |
Ahh, sorry, I should have included a demonstration. Also I notice that the forum software has mangled the code by replacing the ASCII characters with weird unicode ones, even though I enclosed it in code tags… Here is a demonstration program which should work in RISC OS: It generates a picture using pseudo random numbers to decide what colour the pixels should be. It’s not random at all of course, but rather, it’s pseudo-random – it generates a sequence of numbers that don’t have any noticeable pattern to them, but of course they’re completely deterministic. nn1 and nn2 are initialised to 0 implicitly, out of habit I didn’t initialise or declare them because BBC BASIC does that for you. Sorry, not very good for readability… |
David J. Ruck (33) 1636 posts |
BBC BASIC doesn’t do that for you, the program will fail at the point it tries to add a non existent nn1 to itself. |
Patrick M (2888) 126 posts |
I’ve been looking into this, and it seems that it does do it for you as long as you write it like http://dusthillresident.ddns.net/folder/files/quickupload/bbcbasictest.png However, if you write it like http://dusthillresident.ddns.net/folder/files/quickupload/bbcbasictest2.png But apparently only on ARM BBC BASIC. Brandy, Matrix Brandy, and Richard Russell’s BBC BASIC for Windows and BBCSDL all automatically create the variable if it doesn’t already exist. I’ve fixed the code of my program and now it does work on RISC OS. |
David J. Ruck (33) 1636 posts |
Great. Even if you can get away with it on some minor dialects of BASIC, please don’t – it gives people who review code for a living an absolute fit. |
Patrick M (2888) 126 posts |
How could you say that? Richard Russell’s BBC BASIC is no “minor dialect” of BASIC. Outrageous. Edit: Sorry, I meant the tone of this post to be a bit jokey, not seriously angry or anything, but being more serious, I do hold Richard Russell’s BASICs in very high regard… The man is a legend… |
Steve Pampling (1551) 8172 posts |
That sounds like you broke it rather than fixing it. |
Dave Higton (1515) 3534 posts |
Indeed. He is the BBC in BBC BASIC. |