user82580: 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);}
   
   
}``` 


user82594: 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/

82580: <p>Total Arduino noob, I come from a Python &quot;background&quot;, though I barely know that, either.</p>
<p>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 <code>random</code> query numbers? Is it possible to slow it down? I don't need my bulb turning on 50 times a second.</p>
<p>Thanks! And please be kind; I know this is Googleable, I just lack the prerequisite knowledge to Google this efficiently.</p>
<pre><code>/*

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


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

   
}

void loop() {


if(randomNumber &gt; 100)
  digitalWrite(relayPin, LOW);

else{
   digitalWrite(relayPin, HIGH);}
   
   
}``` 
</code></pre>
 82594: <p>There are a few things to be discussed in here.</p>
<p>First, the random number generation: in fact, the arduino <strong>does not</strong> generate random numbers. It has a list of numbers on its memory that just &quot;seems&quot; 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 &quot;random&quot; numbers. This is made with &quot;randomSeed()&quot; in void setup. For example:</p>
<pre><code>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
}
</code></pre>
<p>Next, let's generate our number. The variable can be global (as you declared it outside any function), but <strong>you must declare it with &quot;random()&quot; only inside &quot;void loop&quot;</strong>. Otherwise, its value will be assigned just one time (that's what your code is telling it, to do it once).</p>
<p>Here is what I would do:</p>
<pre><code>int relayPin = 7;
long randomNumber; //just declares the variable; it must be type &quot;long&quot;

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 &gt; 100){
    digitalWrite(relayPin, HIGH);
  }
  else{
    digitalWrite(relayPin, LOW);
  }
  delay(500); //half a second delay
     
}
</code></pre>
<p>If you want to have more details about how random numbers are generated in the Arduino, visit the Arduino language Reference:
<a href="https://www.arduino.cc/reference/en/" rel="nofollow noreferrer">https://www.arduino.cc/reference/en/</a></p>
