coulam123
Published © CC0

Random number generator

Generates a completely random number utilizing an unused pin and the in-built random number generator.

BeginnerProtip2,377
Random number generator

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

introduction-to-arduino-nano-13-1_FILRiaYGHw.png

Code

random number generator code

C/C++
upload this to the arduino and open a serial monitor or plotter.
long randomnum;  //creates the variable randomnum 
int floatingpin = 0;  //creates the variable floatingpin
const int emptypin = A1;  //put an unused analog pin in here
void setup() {
  Serial.begin(9600);  //initialises the serial port
  randomSeed(23); //put any number in here
  pinMode(emptypin, INPUT);  //sets emptypin as an input
}
void loop() {
  floatingpin = analogRead(emptypin);  //sets floating pin to the value of the empty pin
  randomnum = random(2,176);  //put a low number and a high number in here, they are the variables that set the parameters of the randomness.
  Serial.println(randomnum);  //prints out the number
  randomSeed(randomnum/(randomnum*(randomnum*floatingpin)));  //sets the seed to a comletely random number
  delay(randomnum);  //waits for how long the random number is
  
}

Credits

coulam123

coulam123

0 projects • 3 followers

Comments