Random number generator for Arduino?

To my knowledge, Arduino devices have a difficult time creating random numbers with the same random distribution as dice or roulette wheels. I am interested in generating random numbers with Arduino boards and would like your input.

I have seen several different methods for creating pseudo random numbers with varying degrees of quality. Not aware of any of these methods having a flat distribution curve. Meaning the extreme ends (1 and 10 on D10 dice as example) having about equal odds as those in the middle. Is there a pseudo random technique that does this?
Edit: Expecting each number to be independent of those generated before and after in addition to the flat distribution curve.

Would anyone here know of a real random number generator technique compatible with Arduino devices that could be implemented for under $20?

My goal for this discussion is to learn how to implement dice and roulette type games into Arduino projects with minimal expense. Home entertainment type games, not for business or profit purposes.

I see a flat distribution as not random. (these are all flat or non-moving)

2 Likes

In my understanding, flat distribution curve is the ideal random distribution. So, for a coin toss, flat means that both heads and tails occur with nearly equal frequency. Given say 100 coin tosses, both sides will occur very close to 50 times each. Expanded to dice rolls, each number will occur in nearly equal frequency. So for a 10-sided dice and 1000 rolls, each number will occur very close to 100 times.

Using a floating analog input, the initial reading on startup is unknown. However, after the initial reading, the other readings right afterwards will stay very close to the initial reading. Also, the probability of ever seeing 0x00 and 0xFF is extremely low. So, graphing the occurrence of each number will be mostly zero with a single peak with sharp slopes on each side. This is the scenario I don’t want.

I consider real randomness non-homogeneous, like the universe, clumpy.

2 Likes

This search phrase give a number of hits:
"microcontroller true random number generator"

Real-world randomness may be described by the mathematics of probability; however, the probability involved means we will likely only observe a single outcome in our lifetime. For each variable, the quantity of possible outcomes is described by numbers using scientific or engineering notation. The number of variables usually needs the same notation. So, randomness in real life experience cannot be observed as a flat distribution curve. My goal is to implement dice rolls reasonably well in Arduino devices, much simpler.

Yes. It absolutely has hits. However, every single hit explains why “Microcontroller True Random Number Generator” requires external hardware and pseudo random number generators are normally used. I have done the internet searches already and am looking for feedback from experienced Arduino users.

Lava lamps.

1 Like

The Arduino library has a very useful function built in that generates a standard uniform distribution of integer values. What that means is that any particular integer a given range of integers is just as likely to turn up in a given draw from the hat as any other in that range.

You can easily manipulate a series of draws from the uniform distribution to generate any other type of distribution that you wish, e g. Gaussian, Poisson, toss of two dice, spin of the wheel, etc.

If you can be more specific about what you want, describe it and people can help.

As usual with computer random number generators, every time you start up the uniform generator, it produces the same extremely long sequence. A seed function allows you choose a different starting point.

1 Like

stm32g431

Will do. For most things in life, we can pick 2 of 3 from quick, cheap, and easy. I am going for quick and cheap, but still want to discuss all of the random number options.

For my specific application, the random numbers will be mostly for selecting a random time (hours, minutes, perhaps seconds) from within a specified upper and lower limit. This will give the user a random option for a countdown timer. Each time the timer starts counting down, it will need a new set of random numbers. I would prefer for the device to not require a power-cycle reset before each start. Also want to make this standalone so no computer or phone app seeds available.

For hardware, a 32-bit Samd21 (Edit: not Samd3) processor on a custom circuit board running an Arduino sketch is my preference. Want to keep hardware costs to a minimum so multiple examples of it will be affordable.

Please let me know if I missed any information useful for helping me with this project.

That is neat that the STM32 devices have a random number periphery. Is this available in the Atmel SAM range of microcontrollers?

OP can read the micros() at the moment that the button is pushed to roll the dice.
The last 6 or so digits will be pretty random...
The last 2 digits will not be fully random as micros() takes steps of 4.
Use the 6 digits as seed for the random() function.
Costs: 0.

3 Likes
  1. get a geiger counter
  2. get a radioactive source (like from a smoke detector)
  3. count micros between clicks and divide by 4

Thank you for your suggestion. Taking the final 6 digits of millis, dropping the final digit, and storing the remaining digits could work. This could even be done when the random function is selected and later repeated.

Edit: totally misunderstood how the suggestion would function and rewrote this post to reflect the correct understanding.

There will be more random bits than 6. I mentioned 6 digits (in a decimal representation) as it will usually take more than a second to roll the dice again...

I do not understand this bit...

Sorry for the confusion. I completely misunderstood your suggestion and then confused myself further. I corrected the post and edited this post as well.

Thank you for clarifying what you meant by digits earlier. I misunderstood it somehow.

a randomSeed() based on the timestamp when the user has pressed a button or from a floating ADC input should give you enough randomness for this use case.

1 Like

However.

If you toss the coin 100 times then 100 heads is possible although unlikely, if the coin toss is actually random.

What you appear to want is some number generator that is able to control the distribution, i.e if there have been too many heads (for an even distribution) you need to roll over or change some heads to tails.