Over Engineered True Random Value Generator

6.5K4214

Intro: Over Engineered True Random Value Generator

Random numbers are often used in daily for example in encryption, gambling, gaming and much more. These numbers are generated by computers using complex algorithm but it can be predicated if we know the seed value and the algorithm.

So this is my attempt to create over engineered and as far as possible most random number generator.

STEP 1: The Idea

If not algorithms then what should be used to create random. This universe is the most unpredictable thing, then why not use its elements to generate random numbers.

Elements such as pressure, humidity, temperature, vapour pressure and many more. Sensors are available to measure such quantities but we will use ones which don’t need modules to measure.

Basic idea will be to sense a element -> produce a number correspondingly -> then pick a number from a number pool (which also be random one) do some operations over it which will depends on new number produced from element (can you guess what will be a number pool before it is introduced in step 7) and then display it.

STEP 2: Supplies

Zero PCB – 18 x 20 holes

Arduino uno x 1

Male headers – 3 x 1, 1 x 1, 1 x 6

Single strand wire – about 50cm

LDR x 1

2.2K x 1

Electret microphone x 1

4.7K x 1

0.47uF x 1

Push button x 1

OLED display module 128 x 32(pixels) x 1

Female headers – 4 x 1

Solder wire

Soldering iron

STEP 3: Introducing 1st Element

What can be better than a floating pin which picks up stray electromagnetic waves in air?

We just have to attach a wire to an analog pin of uno.

Pin A2 is the floating pin.

First male headers were soldered on PCB.

Headers have been modified, black spacers are pushed down so that metal pins come up and more length can be used for inserting into female headers of uno.

This has to be done because the PCB does not has solder pads on both sides.

I have attached a PCB layout in case you want recreate this project.

Points having distance between them of 5mm on graph paper represent adjacent holes on zero PCB.

STEP 4: Introducing 2nd Element

Next event I would like to introduce is - intensity of light.

Simplest component to generate values using it is a LDR.

Resistance of LDR changes based on intensity of light falling on its surface.

If we create a voltage divider using fixed resistor and LDR then voltage between these two will change which can be detected using another analog pin of Arduino Uno.

I have chosen 2.2k resistor because it is between maximum and minimum values LDR can produce.

One side of LDR is connected to +ve and other to resistor and other side of resistor is connected to ground.

Pin A0 of uno is connected to where LDR and resistor are connected

STEP 5: Introducing 3rd Element

Using an elecret microphone to pick up ambient noises in surrounding would be a good choice.

The mic doesn’t come with any header or extended pins so that we can solder it on PCB but instead has solder pads.

Before adding header to the mic identification of solder pad is necessary, if you closely observe the pads you can see that one of the pad is connected to metal casing of mic using solder masked wires, that one is ground pin and other will be positive.

Then headers were soldered in a little tiled manner to the electrets microphone.

Appropriate components were chosen like 4.7K resistor to limit current to min and 0.47uF to filter out the DC section of signal.

Then all components were soldered on the PCB according to the layout.

Pin A1 will pick up the ambient sounds.

STEP 6: Putting All Together

The last step in putting things all together will be adding a display module to show the random number generated and to add a button to trigger or say to tell the system that user wants a random number.

I have used an OLED 128 x 32 display module and push button.

Button is directly soldered on PCB but display module is attached using male female header combination so that it can be removed whenever required.

PCB is so designed that it fits over Arduino Uno like a shield.

STEP 7: Pi Bank

I could have simply taken values from pins and present it as this even keeps the numbers generated to be random but there is no fun and very straight forward.

So to give a little twist why no use the digits of pi which are completely random and never ending.

Being the digits completely random there are few methods using which we can find the digits like the Gregory-Leibniz series: pi/4 = 1-1/3 + 1/5 – 1/7 + 1/9 - ....

But there is no use in doing the work which has already been done.

We can get all the digits of pi here but I will only use up to 512 and why I have done this you will understand in next step.

STEP 8: The Code

Now is the time to create crazy enough logic to work upon these numbers.

First an array will be created where all the digits of pi will be stored sequentially.

Now you might wonder why only 512 digits are used this is because of low memory in Arduino Uno.

Arduino Uno has about 2KB of memory for keeping variables.

So if we use 1023 corresponding to ADC of Uno then half of the ram will be consumed by the array.

Array and remaining variables consume about 75 % of the ram which leads to astability of the code so size of array has to be reduced.

Reducing to half works pretty fine.

After creation of array it takes values from elements, reduces that values using map function, extracts numbers from pi bank according to received values form elements and then works upon them further.

Flowchart shows very well what the code does.

what interesting part is that the code is so setup that when button is pressed it generates random number every 100ms and displays it and when you release the button it stop at one particular number so kind of gives a rattling effect.

STEP 9: Done!

And we done here each time button is press it generates a random and displays it on OLED display module.

STEP 10: What Next?

As an improvement more elements can be added and more crazy logic can be created.

12 Comments

A nice Pi project. Given the hardware limitations it is a well thought out solution.
My comments below are for interest and should not be taken as criticism.

John von Neumann famously said "Anyone who attempts to generate random numbers by deterministic means is, of course, living in a state of sin."

By combining a deterministic (algorithmic) method with data obtained from notionally random environmental variables you appear to have avoided the state of “living in sin”.

Before you look to improve this project perhaps you might benefit from reviewing how good it is. Have you done any statistical tests of the randomness of the output?

As a part of this is about the fun of using Pi and Pi day I will comment on the nature of Pi.

Below I will refer to the number of digits of Pi as N and in this project, N is 512. There are 10 digits 0 – 9 and I will represent this with D = 10.


Is Pi really random and how good is it for making random digit selections? Well sort of.
It is not possible to say yes absolutely as we can never know all the digits of what is an infinite series. In practical terms it is random but with certain limitations. These limitations are more significant when N is small.

This leads to the next question which is whether Pi is Normal. What does this mean? It means that each digit occurs exactly N/D times so if we looked at the first 100 digits of Pi we would expect to find exactly 10 of each digit. However, this is not what we find.

To illustrate this, I have made a table that shows the frequency and percentage occurrence of each digit 0 - 9 in the first 500 and 1000 digits of Pi.


For example, in 2003, Yasumasa Kanada published the distribution of the number of times different digits appear in the first trillion digits of pi. Event with N being so vast the distribution is very slightly uneven.

If we go to digit 768 we find the start of a sequence of 9 repetitions of 9. This is called the Feynman Point after Richard Feynman. If we start at position 17,387,594,880 we find a sequence “0123456789”.

The quality of a random number generator can be determined by a statistical analysis of its output. Despite the logical gymnastics of the program the statistical pattern of Pi will ‘bleed through’ to the output. With a sufficiently large volume of output the randomness might be compromised somewhat.

So, if you want to use this as an electronic dice to make random seeming decisions in say a game or a robot navigation system it is probably adequately ‘over engineered’.
On the other hand, if you wanted to use it as the basis of a cryptographic system with a nation’s future at stake you might want to reconsider.

All done :-)

Yes, Jeremy, but . . . ! Apparent repetition can come from randomness !

If you throw dice repeatedly for long enough, it becomes increasingly likely that you will eventually throw six sixes in a row, or even 1, 2, 3, 4, 5, 6 in sequence.

(In fact it is equally likely to be the first numbers you throw !)
Things you mentioned are deep, although I have not performed any statistical test but will consider the points you mentioned.
Thank you
Would it be a good idea to add the function of outputting the number to a PC ?
This would save you from 1) manual copying, 2) manual errors, each time a number was generated.
manual error could be another randomising factor
Can you elaborate what do mean?
It would be good to have a cable (and sofware) that would plug into a PC, to communicate the generated random numbers automatically, perhaps into a spreadsheet or other computer program.
For anyone looking to make it - it's a cool project but terrible random number generator.
Sources of randomnes like sound or light need special post processing or they _will_ have terrible biases.
Using PI as a lookup table also has it's issues as it's not unbiased either (it's only unbiased in infinity).
Issues you mentioned cannot be ignored. Will definitely work upon them.
I would prefer a geiger counter output as "floating point". The time between the detections of particles is really statistical,
Best regards
Eric
Geiger counter is an area to explore. Using such might be an overkill.
Awesome project, I thought the mathematical aspect of the code was really interesting even thought it’s a little beyond me! Led me on a long rabbit trail of researching sequences like the Gregory-Leibniz series...