All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

JavaScript Random Number Between 1 And 10

Last Updated : Mar 11, 2024

JavaScript Random Number Between 1 And 10

In this tutorial we will show you the solution of JavaScript random number between 1 and 10, JavaScript is considered as a full-featured programming language capable of swiftly and accurately performing complex mathematical calculations.

The Math object is made easier to accomplish these computations thanks to the JavaScript standard.

The Math object is frequently used by programmers to generate random numbers. Randomness is employed in art, science, and gaming on a regular basis.

Random numbers are required whether you're recreating the stroke of a sable paintbrush, conducting a controlled medical experiment, creating an online casino, or encrypting anything.

Creating JavaScript random numbers isn't the same as simply assigning a number to a variable within a programme; there are a few extra steps involved which are listed below

Step By Step Guide On JavaScript Random Number Between 1 And 10 :-

Following that, this new pseudorandom-number generator is used for all calls to this method and nowhere else. This technique is appropriately synchronised to allow many threads to use it correctly.

If several threads need to generate pseudorandom numbers at a high pace, having each thread's own pseudorandom-number generator may minimize contention.

So, here's a JavaScript programme to produce a random integer between 1 and 10.

<!DOCTYPE html>
<html>
<head></head>
<body>
<script>
var randomNum = Math.floor(Math.random() * 10) + 1;
console.log(randomNum)
 </script>
</body>
</html>
  1. In javascript, we may use the Math.random() method to obtain a random number between 1 and 10.
  2. Math.random() gives a value that is between 0 and 1. As a result, Math.random() always returns an integer less than 1.
  3. To generate a random integer, we can combine Math.random() and Math.floor().

The following is a generic formula for generating a random number inside a range.

Math.floor(Math.random() * (maximum – minimum + 1)) + minimum
In our case,
minimum = 1
maximum = 10
It turns out to be:
Math.floor(Math.random() * (10 – 1 + 1)) + 1
Math.floor(Math.random() * 10) + 1
  1. The java.lang package.Math.random() returns a double positive-sign value that is higher than or equal to 0.0 but less than 1.0.
  2. The returned values are picked pseudorandomly from that range with a (roughly) uniform distribution. After this method is called for the first time, it creates a single new pseudorandom-number generator, just like new java.util. Random.

Way to generate 10 random integers in range of 1 to 10

<!DOCTYPE html>
<html>
<head></head>
<body>
<script>
console.log("Way to generate 10 random integers in range of 1 to 10")
for (let i = 0; i < 10; i++) {
  var randNum = Math.floor(Math.random() * 10) + 1;
  console.log(randNum)
}
 </script>
</body>
</html>
  1. To produce 10 random integers, we can just use a for loop to iterate over the above code.

Conclusion :-

It's more difficult to create random numbers with Javascript than it appears. They are a simple Javascript concept that, when generated for certain conditions, requires some additional conceptual expertise.

We've looked at the logic and different methods for generating pseudo-random integers with Math.Random in this tutorial. I hope this tutorial on JavaScript random number between 1 and 10 helps you.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪