How to Generate Random Number in React JS?
Last Updated :
24 Sep, 2024
This article explores generating random numbers in React.js, accomplished through the utilization of the Math.random() method.
Prerequisites:
Approach
To generate Random numbers in React JS we will be using the Math.random and Math.floor methods as mentioned below.
- The Math.random() method in JavaScript returns a random number between 0 (inclusive) and 1 (exclusive).
- The Math.floor() function in JavaScript is utilized to round off a given input number to the nearest smaller integer in a downward direction. In other words, it truncates the decimal part of the number.
Syntax:
Math.floor(Math.random());
Steps to Create React App
Step 1: Create a react application by using this command
npx create-react-app react-project
Step 2: After creating your project folder, i.e. react-project, use the following command to navigate to it:
cd react-project
Project Structure:
Project StructureThe updated dependencies in package.json file will look like:
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4",
}
Example: The "App" function utilizes useState to create a "num" state with an initial value of 0, includes a "randomNumberInRange" helper, and updates "num" on button click to display a random number between 1 to 20 in an h2 element inside a styled wrapper div.
CSS
.wrapper {
padding: 20px;
text-align: center;
}
h2 {
font-size: 24px;
margin-bottom: 10px;
}
button {
background-color: green;
color: white;
padding: 10px 20px;
border-radius: 5px;
border: none;
font-size: 16px;
cursor: pointer;
}
button:hover {
background-color: #3e8e41;
box-shadow: 1px 1px 10px 1px rgb(185, 185, 185);
transform: translate(0px, -2px);
}
JavaScript
// Filename - App.js
import React, { useState } from "react";
import "./App.css";
const App = () => {
const [num, setNum] = useState(0);
const randomNumberInRange = (min, max) => {
return Math.floor(Math.random()
* (max - min + 1)) + min;
};
const handleClick = () => {
setNum(randomNumberInRange(1, 20));
};
return (
<div className="wrapper">
<h2>Number is: {num}</h2>
<button onClick={handleClick}>
Click Me Generate
</button>
</div>
);
};
export default App;
Step To run the application: Open the Terminal and enter the command listed below.
npm start
Output:
Output
Similar Reads
How to Generate Random Password in ReactJS ? Generating random passwords is basic requirement for applications that has user authentication, registration, or security-related functionalities. In ReactJS, you can create a simple function to generate strong and random passwords for users. In this article, we will guide you through the process of
3 min read
How to Generate a Random Number in JavaScript? To generate a random number in JavaScript, use the built-in methods that produce a floating-point number between 0 (inclusive) and 1 (exclusive).Below are the approaches to generate random numbers in JavaScript:Table of ContentUsing Math.random() methodUsing Math.floor() with Math.random()Using Math
2 min read
Generate Random Number in Given Range Using JavaScript Here are the different ways to generate random numbers in a given range using JavaScript1. Using Math.random(): basic ApproachThis is the simplest way to generate a random number within a range using Math.random().JavaScriptlet min = 10; let max = 20; let random = Math.floor(Math.random() * (max - m
3 min read
How to generate random colors by using React hooks ? In web development, colors play a vital role in creating visually appealing and engaging user interfaces. In React we may need to generate random colors dynamically. In this article, we will explore how to achieve this using React hooks, a powerful feature introduced in ReactJs. Pre-requisite:NPM
2 min read
Generate random alpha-numeric string in JavaScript In this article with the help of javascript we are generating the alpha-numeric string of specific length using javascript, here are some common method Approach 1: Creates a function that takes 2 arguments one is the length of the string that we want to generate and another is the characters that we
2 min read
Build a Random Name Generator using ReactJS In this article, a Random Name GeÂnerator will be created using React.js. Building a Random Name Generator means creating a program or application that generates random names, typically for various purposes like usernames, fictional characters, or data testing. It usually involves combining or selec
4 min read