# Using Random() and Greater Than

Total Arduino noob, I come from a Python "background", though I barely know that, either.

I'm writing a super simple script that will turn on a bulb when the random number generator generates a number greater than 100. Obviously, I have a syntax problem or something: What am I doing wrong? Secondly, How quickly does `random` query numbers? Is it possible to slow it down? I don't need my bulb turning on 50 times a second.

Thanks! And please be kind; I know this is Googleable, I just lack the prerequisite knowledge to Google this efficiently.

``````/*

*/
int relayPin = 7;
int randomNumber = random(1001);

void setup() {
pinMode(relayPin, OUTPUT);

}

void loop() {

if(randomNumber > 100)
digitalWrite(relayPin, LOW);

else{
digitalWrite(relayPin, HIGH);}

}```
``````
• what is the problem? Feb 1, 2022 at 1:05
• btw, put the ``` on a separate line so that it is invisible Feb 1, 2022 at 1:06
• The problem is that the relay module does not switch. I assume that there is an error in my code. Feb 1, 2022 at 2:35
• yes, logic error ... ask yourself `when is the value of randomNumber updated?` Feb 1, 2022 at 3:27
• The current code (if you move the random number generation into loop()) will switch not just 50 times, but thousands of times per second. Look at some example code to get an idea about how to solve that. Feb 1, 2022 at 6:55

There are a few things to be discussed in here.

First, the random number generation: in fact, the arduino does not generate random numbers. It has a list of numbers on its memory that just "seems" to be random. That's why it's called a pseudo-random number. So, you need to declare a starting point on this sequence, from which you will get the "random" numbers. This is made with "randomSeed()" in void setup. For example:

``````void setup() {
// put your setup code here, to run once:
randomSeed(analogRead(0)); //starts the pseudo-random sequence in a random point,
//in this case, the analogRead value of an unconnected pin
}
``````

Next, let's generate our number. The variable can be global (as you declared it outside any function), but you must declare it with "random()" only inside "void loop". Otherwise, its value will be assigned just one time (that's what your code is telling it, to do it once).

Here is what I would do:

``````int relayPin = 7;
long randomNumber; //just declares the variable; it must be type "long"

void setup() {
// put your setup code here, to run once:
randomSeed(analogRead(0)); //starts the pseudo-random sequence in a random point
pinMode(relayPin, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
randomNumber = random(0, 301);
//each time the code loops, it assings another value to randomNumber
//in this case, the number will be between 0 and 300 (301 is the excluded limit)

if(randomNumber > 100){
digitalWrite(relayPin, HIGH);
}
else{
digitalWrite(relayPin, LOW);
}
delay(500); //half a second delay

}
``````

If you want to have more details about how random numbers are generated in the Arduino, visit the Arduino language Reference: https://www.arduino.cc/reference/en/

• Thank you for this! This explanation was very kind and helpful of you! I will implement this after work. Feb 1, 2022 at 20:30