JIYIK CN >

Current Location:Home > Learning > PROGRAM > C++ >

Generating Random Numbers in Arduino

Author:JIYIK Last Updated:2025/04/16 Views:

random()This tutorial will discuss generating random numbers using the function in Arduino .


random()Generate random numbers in Arduino using the function

We use a random number generator to generate a random number within a given range of numbers. For example, we can use it to pick a winner for a giveaway.

We can random()generate random numbers in Arduino using the function.

grammar:

random(maxVlaue);
random(minValue, maxValue);

In the first line of code, if we only pass the maximum value of the range, the function will use zero as the minimum value. We can also random()define the minimum and maximum range using the first and second arguments of the function.

The minimum value of a range is inclusive, while the maximum value is exclusive. For example, if we define a range from 0 to 10, the random number sequence will include 0 but not 10.

Before using random()the function, we have to randomSeed()initialize it using the function. We have to randomSeed()pass a random number longdata type in the function to initialize the random number generator.

Suppose we want the random number to be the same sequence of numbers as the previous one. We have to randomSeed()pass the same number in the function.

If we want a different sequence of numbers, we have to use a different number each time we initialize the random number generator. In this case, we can use any analog pin of the Arduino.

When no input is connected to an analog pin, the pin will have a floating point value or a random value. We can analogRead()read this random value using the function.

For example, let's random()generate a random number between 0 and 100 using .

long MyRnd;

void setup() {
  Serial.begin(9600);
  randomSeed(analogRead(0));
}
void loop() {
  MyRnd = random(100);
  Serial.println(MyRnd);
  delay(500);
}

Output:

21
17
20
11
46
51
41
71
2
74

The random numbers will continue to be generated because we placed the random number generator in loop()the function. If we wanted to generate random numbers a specific number of times, we could setup()use a loop in the function, which would only run once.

The range value we random()defined in the function can be 32 bits. If you use a larger value, the function will not fail, but the result will be different from what you expect.

If we randomSeed()use 1the same number like in the function and restart the code, the sequence of random numbers will be the same.

Previous: None

Next:Arduino mills() function

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Stopping a loop in Arduino

Publish Date:2025/04/16 Views:150 Category:C++

This tutorial will discuss the methods to stop a loop in Arduino. There are two types of loops in Arduino: one is the void loop() provided by default and the other is created by the user in it. The loop created by the user can be ended easi

Arduino prints to console

Publish Date:2025/04/16 Views:68 Category:C++

This tutorial will discuss printing text or variables on the console using the Arduino IDE's serial monitor. Arduino using the serial monitor to print to the console The Arduino IDE has a console at the bottom, but we cannot print anything

Arduino Array Length

Publish Date:2025/04/16 Views:181 Category:C++

Arrays are fundamental data structures in programming, and in Arduino, they play a key role when storing and manipulating data. Often, you'll find yourself needing to know the size or length of an array, especially when working on complex p

Arduino 2D Array

Publish Date:2025/04/16 Views:108 Category:C++

In this tutorial, we will discuss about 2D arrays in Arduino. We will discuss how to initialize a 2D array and use it to store data. 2D Array Initialization in Arduino Two-dimensional array initialization is very similar to one-dimensional

Printing Character Array in Arduino

Publish Date:2025/04/16 Views:59 Category:C++

This tutorial will discuss printing character array using loop in Arduino. Serial.println() Define int and print character arrays in Arduino In Arduino, if we int initialize an array using keyword, we have to use a loop to print its element

Arduino Square Wave Generator

Publish Date:2025/04/16 Views:181 Category:C++

digitalWrite() This tutorial will discuss generating a square wave using the function in Arduino . Arduino Square Wave Generator A square wave consists of maximum and minimum values, and the transitions between these values ​​are instan

Splitting a string in Arduino

Publish Date:2025/04/16 Views:188 Category:C++

substring() This tutorial will discuss splitting a string using the function in Arduino . substring() Splitting a string in Arduino using the function Arduino provides an inbuilt function substring() to split a given string. We can split th

Comparing Strings in Arduino

Publish Date:2025/04/16 Views:137 Category:C++

compareTo() This tutorial will discuss comparing two strings using the function in Arduino . compareTo() Comparing strings using the Arduino function To compare two strings in Arduino, we can use compareTo() the function of the string objec

Concatenating strings in Arduino

Publish Date:2025/04/16 Views:190 Category:C++

This tutorial will discuss concat() concatenating two strings using the function or the append operator in Arduino. concat() Concatenate strings using the Arduino function We can concat() concatenate two strings in Arduino using the concate

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial