By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript Program > Write a Proram to Generate a Random Number in JavaScript
JavaScript Program

Write a Proram to Generate a Random Number in JavaScript

Rocoderes
Last updated: 2022/09/07 at 3:07 PM
Rocoderes
Share
5 Min Read
Write a Proram to Generate a Random Number in JavaScript

In this article, we will generate a random number in JavaScript. We will use the Math() library which is provided by JavaScript. Since we know that Math library contains functions who are actually related to mathematics. So we will here use the Math.random() function to get random number, but random() function only returns float values which ranges between 0 and 1. This is why we need to customize this, so it should any random value.

Contents
Generating Normal Random Value Between 0 and 1Generating The Within RangeGenerating Random Integer ValueOutputYou may also like:

This program is very basic and for beginners, so let’s just make this and understand with clear and basic language.

Generating Normal Random Value Between 0 and 1

Firstly, we need to add HTML file, in this file we will add normal HTML markup. Now we have added a <script> tag in which we will add our JS code. Here we have added a variable in which we have assigned value from Math.random a function. So in this variable, we have a random float value between 0 and 1. And we just used the document.write method to show the result. You can notice here we have added <h1> inside the JS code and used $ sign for our variable, these two things will work when we use `` (backtick).

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
        var x = Math.random();
        document.write(`<h1>generated value: ${x}</h1>`);
    </script>
</body>
</html>
Write a Proram to Generate a Random Number in JavaScript

As we can see, we are able to generate random number and if we refresh then we get different and random values.

Generating The Within Range

We have here copied the above code, and we have just added one more line here, we have just multiplied the value with 100. This is because we need a value between 0 and 100, that means you just need to multiply the highest value of range? So yes, for example, if we require a random value between 0 and 1000 then you just need to multiply by 1000. Now if we need a value between 20 and 500, then we need to multiply then value with 500 and add 20. Like x = (x * 500) + 20;. Remember, all of these value will be in float, means decimal values will be in there.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
        var x = Math.random();
        document.write(`<h1>generated value: ${x}</h1>`);
        x =  x * 100;
        document.write(`<h1> Ranged generated value: ${x}</h1>`);
    </script>
</body>
</html>
Write a Proram to Generate a Random Number in JavaScript

Generating Random Integer Value

Now we have again copied the above code, and again we have added a single line of code. In here, we have applied the Math.floor() function on our variable to get an integer value. Floor function will remove the decimals and show only integer. Here one more thing which I want to point out is, we can also use parseInt() to get integer value, but it won’t be good practice, because parseInt() converts the string to integer number. So it’s better to use floor function.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
        var x = Math.random();
        document.write(`<h1>generated value: ${x}</h1>`);
        x =  x * 100;
        document.write(`<h1> Ranged generated value: ${x}</h1>`);
        x= Math.floor(x);
        document.write(`<h1> Integer Ranged generated value: ${x}</h1>`);
        
    </script>
</body>
</html>

Output

Write a Proram to Generate a Random Number in JavaScript

You may also like:

  • How to Check If a Number is Positive, Negative, or Zero
  • How to Convert Kilometers to Miles and Celsius to Fahrenheit
  • How to Add Two Numbers, Swap Two Numbers And Find Square Root in JavaScript

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

TAGGED: programs
Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Posted by Rocoderes
Rocoderes is a blog you can learn HTML, CSS, JavaScript, React Js and Python along with creative coding stuff and free source code files.
Previous Article How to Check If a Number is Positive, Negative, or Zero How to Check If a Number is Positive, Negative, or Zero
Next Article Write a program to Check if a Number is Odd or Even in JavaScript Write a program to Check if a Number is Odd or Even in JavaScript
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

Computing Fibonacci Sequence in JavaScript

Computing Fibonacci Sequence in JavaScript

January 12, 2023
Swap Two Numbers Without A Third Variable

How to Swap Two Numbers Without A Third Variable in JS

December 12, 2022
Armstrong Number

How to Find an Armstrong Number Using JavaScript

September 23, 2022
Print The Table

How to Print The Table of Any User Defined Number Using Function

September 22, 2022
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?