- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Generate random numbers in Arduino
Generating random numbers is one of the key requirements from microcontrollers. Random numbers have several applications. Let’s not get there. You must have an application in mind, which brought you to this page. Generating random numbers is very easy in Arduino, thanks to the inbuilt random() function.
Syntax
random(min, max)
OR
random(max)
where min is 0 by default.
Min is inclusive, while max is exclusive. Thus, random(10,50) will return a number integer between 10 and 49 (10 and 49 included). random(100) will return a random number between 0 and 99, both included. Note that the random function’s return type is long.
Example
void setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println(); long r1 = random(100); Serial.println(r1); } void loop() { // put your main code here, to run repeatedly: }
Output
The Serial Monitor output is shown below −
Ignore the garbage output. Every time the board is reset, some garbage gets printed. But you can see that the random number printed every time is different.
- Related Articles
- Generate pseudo-random numbers in Python
- Generate Random Integer Numbers in Java
- Generate random numbers using C++11 random library
- Generate Random Long type numbers in Java
- Generate random characters and numbers in JavaScript?
- Java program to generate random numbers
- How does Python generate random numbers?
- Excel random data: generate random numbers, texts, dates, times in Excel
- How to generate random numbers between two numbers in JavaScript?
- Generate 10 random four-digit numbers in Java
- How to generate large random numbers in Java?
- Generate array of random unique numbers in PHP?
- C# program to generate secure random numbers
- Java Program to generate random numbers string
- Python module to Generate secure random numbers
