![]() ![]() UpperBound = seedValue / 127773L // Create an upper bound to prevent overflowing 31 bits If (seedValue = 0) seedValue = 123459876L // Prevent the Seed Value from being zero I’ve culled out the relevant source code for our viewing here, and cleaned it up a bit to make it more legible. As of this writing, the Arduino IDE uses avr-libc v2.0.0, which, if you really feel like it, can be downloaded here: and I’ve included the relevant source code at the bottom of this post for easy reference (and because it’s copyrighted, so want to give The Regents of the University of California their due). That chunk of avr-libc code comes precompiled with the Arduino IDE installation, so you can’t just search for the source on your machine to view it. When you execute the random() command in your code, you are running something actually located in the wmath.cpp file, but that just does some checking to make sure the submitted values were formatted correctly, then passes it on to the stdlib.h file, which then uses the source code in the avr-libc standard libraries to do the heavy lifting. ![]() Consider that I only went down this path because I thought it would be cooler to display raffle ticket numbers on a seven segment display than just picking the matching ticket out of a bag. I can understand the barest surface of the theory behind random number generation true, psuedo or otherwise. I do have some caveats to make: I am not a mathematician, nor a cryptologist, number theorist, or pretty much anything that ends with “ist” (I am quite a few things that end with “er”). ![]() I started pulling on that thread and this is the result. I had seeded it with an analogRead of A0, and yet the same values had popped up. I know better than to simply use random() to generate a number, because the internal algorithm produces a fixed sequence of numbers, and you have to seed it with some value to get true psuedo-random numbers. I immediately noticed that what I thought would be random ticket numbers returned, turned out to be not so random after all: on tries 2, 3 and 5, the same ticket number appeared. My path to finding better Arduino random numbers, started with wanting a bit of code to pick winning raffle ticket numbers and display it on the I2C Display Add-on. ![]()
0 Comments
Leave a Reply. |