Hello guys! Welcome back to my blog today we are going to see what random function in Arduino is.
First of all, a random number means any number which is not in
series, these numbers are unpredictable and chosen without any sequence.
Syntax –
- random(max)
- 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.
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);
}
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.
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.
Code -
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
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.