What is random() function in Arduino??

What is a random function in Arduino, how to generate a random number in Arduino & how to use a random function in Arduino are some questions that may arise while programming an Arduino.

Hello guys! Welcome back to my blog today we are going to see what random function in Arduino is.

random Arduino

First of all, a random number means any number which is not in series, these numbers are unpredictable and chosen without any sequence. 

Syntax –

  1.       random(max) 
  2.       random(min, max)

         both random functions are the same 

  •       random(max) 

In the first one, you can pass one argument that will be the maximum number, which means if you pass the value 10 random (10) then it will generate a random number from 0 to 9.

  •       random(min, max)

In the second one you can pass two arguments from the minimum value or value you start from to the maximum value means if you pass the value random (10,30) then it will generate a random number from 10 to 29.

The data type we used to store random numbers is long

long RandomNumber = random(10, 30);

This random function returns random values from 10 to 29 and is stored in this variable name RandomNumber.

long RandomNumber;
void setup() 
{
 Serial.begin(115200);
 
}
void loop() 
{
  
 //RandomNumber = random(30);
 // print a random number from 10 to 30
  RandomNumber = random(10, 30);
 Serial.print("Random Number is : ");
  Serial.println(RandomNumber);
  delay(1000); 
}

random number generator Arduino

In the serial monitor, we got all random numbers but we have noticed these random values repeat themselves. example number 19 is shown in the output. So the solution to this is we can use randomseed() with analogRead() on an unconnected pin.

randomSeed(analogRead(A0));

By using this randomseed() function we can see all repeated values are gone. Only unique random value.

Generating non-repetitive random number using randomSeed() function

long RandomNumber;
void setup() 
{
 Serial.begin(115200);
  randomSeed(analogRead(A0));
}
void loop() 
{
  
 //RandomNumber = random(30);
 // print a random number from 10 to 30
  RandomNumber = random(10, 30);
 Serial.print("Random Number is : ");
  Serial.println(RandomNumber);
  delay(1000); 
}

Now we will see how to generate a non-consecutive repetitive number. So we will use the condition statement and if the number is the same then we will update it.

Generating non-repetitive random number with if else condition and random()

long RandomNumber;
long laststate;

void setup() 
{
 Serial.begin(115200);
}

void loop() 
{
  
 //RandomNumber = random(30);
 // print a random number from 10 to 30
  RandomNumber = random(10, 30);
  if(RandomNumber == laststate)
  {
    Serial.print("Number is Repeated : ");
    Serial.print(RandomNumber);
    RandomNumber = "";
    RandomNumber = random(10, 30);
   }
 Serial.print("Random Number is : ");
  Serial.println(RandomNumber);
   laststate = RandomNumber;
  delay(1000); 
}

Blinking Random LED using random() with Arduino

LED blinking randomly Arduino

Arduino random number example
This image is created using Fritzing

Code - 


int ledpins[] = {2,3,4,5,6,7,8,9,10,11};
int total_ledpin = 10;
long RandomNumber;

void setup() 
{
 Serial.begin(115200);
 for (int i=0;i<=total_ledpin;i++)
 {
  pinMode(ledpins[i],OUTPUT);
  }

}

void loop() 
{
  
 //RandomNumber = random(11);
 // print a random number from  to 11
  RandomNumber = random(2, 11);
  Serial.println(RandomNumber);
  digitalWrite(RandomNumber,HIGH);
  delay(1000);
  digitalWrite(RandomNumber,LOW); 
  
}

Here we have created an array and assigned a name of the array as ledpins. Here we are using a total of 10 pins of Arduino to connect 10 LEDs. We are connecting LED to Arduino pin numbers 2, 3, 4, 5, 6, 7, 8, 9, 10, 11. We have declared the total LED pin count as 10 as in this example we are using 10 LED. we have initialized the variable name RandomNumber to store random values which are returned from random() function.

In the void setup, we have configured the baud rate to 115200. We configure pins as INPUT or OUTPUT. We have to declare the LED pin as output. Here we have 10 LEDs so instead of declaring each 10 LED pin as output. We will use for loop to declare LED pins. We will discuss for loop in detail. In for loop, we initialize the value of i as 0 first and later increment till the value of i reaches the total length of LED which we have declared as 10 (i = 0 to i =10). 

In the void loop, we are storing the value generated by the random() function which returns random integer values from 2 to 11 in a variable, which we will represent by Arduino pins. Inside the digitalWrite function, we pass a variable that consists of a random number that will be served as an Arduino pin. To turn LED ON we will pass a variable containing a random number with the keyword HIGH inside digitalWrite() and to turn LED OFF we will pass a variable containing a random number with the keyword HIGH inside digitalWrite().

Conclusion –

Today we will learn about for random() function, random() function in Arduino, what is random() function, how to use random() function in programming. we have seen an example of a random() function using multiple LEDs. random() functions are most commonly used in each and every programing language.

"I hope you find this IOT blog very helpful to you. In the upcoming lesson, we will see about for loop till then bye. See you all in my next blog." 

Close Menu