An Arduino-based random number generator

Created on Sunday, September 1, 2013.
 

A random number generator gadget built on Arduino. It’s way over-engineered and that is how I like it.

 

What is it?

Randuino is a gadget that produces pseudo-random numbers and displays them on an LCD screen. It’s built on the Arduino platform. It’s functional, but not ready for actual use.

The goal is to have a ruggedised random number tool for scientific fieldwork, one that I don'’t have qualms giving to other students, and which can take the place of the random number app on my phone. I could use a printed random number table, but that is nowhere near over-engineered enough for me.

The Arduino’‘s documentation suggests that its analog pins can detect the field of cosmic/microwave/radio waves around us, express it as a number from 0–1023, and then use that number as the seed for random number generation. However, I’’ve noticed that the seed value generally ranges from 100-500. I’’ve tried to extend that range by using the seed value to randomly produce another seed, which is then used as the actual seed for producing the random numbers that are displayed to the user. Phew!

Materials

Note: All of this stuff (except for the 110 Ω resistor) can be found in the official Arduino Starter Kit. You can use one of the included potentiometers instead.

  • 1x 110 Ω resistor.
  • 1x 220 Ω resistor.
  • 1x 100k Ω resistor.
  • 1x momentary switch.
  • 1x LCD (16 x 2).
  • Jumper wires for breadboarding.
  • An Arduino, of course. I used the Uno.

Pinout (click to embiggen!)

Code

// Random number generator v0
// Desi Quintans (desiquintans.com/randuino)

#include <LiquidCrystal.h>

LiquidCrystal lcd(4, 5, 6, 7, 8, 9);

const int seedPin = A0;
const int button = 2;
int buttonState;
int prevButtonState;
int totalNumbersGenerated = 0;

void setup() {
    
    lcd.begin (16,2);
    
    pinMode (seedPin, INPUT);
    pinMode (button, INPUT);
    int randSeed = analogRead (seedPin);
    int generatedSeed = random (-randSeed, randSeed);
    randomSeed (generatedSeed);
    prevButtonState = digitalRead (button);
    
    PrintToLcd (
        "Rng: " + String (-randSeed) + " - " + String (randSeed),
        "Seed: " + String (generatedSeed)
        );
}

void loop() {
    buttonState = digitalRead (button);
    if (buttonState != prevButtonState && buttonState == HIGH)
    {
        totalNumbersGenerated++;
        PrintToLcd (
            "Result #" + String (totalNumbersGenerated),
            String (random (-999999999, 999999999))
            );
        delay (200);
    }
    prevButtonState = buttonState;
}

void PrintToLcd (String line1, String line2)
{
    lcd.clear ();
    lcd.print (line1);
    lcd.setCursor (0, 1);
    lcd.print (line2);
}

Additions and changes I’’d like to make

This thing is at a preliminary stage, and I’’m documenting it here so that I can do other things with my only Arduino, and reassemble this later. Things I’’d like to implement later are:

  1. Append new random numbers to the screen instead of clearing each time and displaying only one.
  2. Add a toggle for generating unique numbers only.
  3. Add a switch to clear the screen and stored data.
  4. Add a membrane keypad so that I can:
    1. Define a custom range of numbers (including negatives).
    2. Streamline the user interface (by using states instead of tons of momentary switches).
  5. Store user settings (neg/pos toggle, custom range, unique results) in EEPROM, with an option to clear data.
That's all there is, there isn't any more.
© Desi Quintans, 2002 – 2018.