Navigate back to the homepage

how to generate random number in react js?

Infinitbility
React
July 5th, 2022 · 1 min read
how to generate random number in react js?

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To generate a random number in react js, use the Math.random() method it will generate a random number.

In the following example, we set the min and max number with the Math.random() method so it will generate random numbers in the range of min and max number.

1function getRandomNumber() {
2 let min = 1000;
3 let max = 9999;
4 return Math.round(Math.random() * (max - min) + min);
5}
6
7getRandomNumber(); // return 4 char random number

Today, I’m going to show you How do i generate random numbers in react js, as mentioned above here, I’m going to use the Math.random() method.

Let’s start today’s tutorial how do you generate random numbers in react js?

In this example, we will do

  1. take an example of a number state which can store a random number
  2. create a function that generates 4 digits random number
  3. call the function with the click of a button
  4. print the output of the string on the page screen
1import React, { useState, useEffect } from "react";
2export default function App() {
3 const [sampleNumber, setSampleNumber] = useState(null);
4
5 const getRandomNumber = () => {
6 let min = 1000;
7 let max = 9999;
8 setSampleNumber(Math.round(Math.random() * (max - min) + min))
9 }
10
11 return (
12 <div className="App">
13 <h1>{`Genrate random number on click of the button`}</h1>
14 <button onClick={() => getRandomNumber()}>{'Genrate'}</button>
15 <p>{sampleNumber}</p>
16 </div>
17 );
18}

In the above react js example, we have taken the sample number state, and create a function which can genrate random number with in range and store in the state.

I hope it helps you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to add two numbers in react js?

how to add two numbers in react js?

An example of add two numbers in react js with a step-by-step example.

July 4th, 2022 · 1 min read
how to replace string in react js?

how to replace string in react js?

An example of replace string in react js with a step-by-step example.

July 4th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]