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/
when is the value of randomNumber updated?