Generate random number between 1 and 10 in javascript

In this post, we will see how to generate random number between 1 to 10 in javascript. How to generate random number between 1 and 10 in javascript We can simply Math.random() method to generate random number between 1 and 10 in javascript. `Math.random()` returns a random number between 0(inclusive), and 1(exclusive). That means `Math.random()` returns always number lower than 1. We can use `Math.random()` with `Math.floor()` to generate random integer. Here is generic formula to generate random number in the range. Math.floor(Math.random() * (maximum - minimum + 1)) + minimum In our case, minimum = 1 maximum = 10 so it will be Math.floor(Math.random() * (10 - 1 + 1)) + 1 Math.floor(Math.random() * 10) + 1 So here is the program to generate random number between 1 and 10 in javascript. var randNum = Math.floor(Math.random() * 10) + 1; console.log(randNum) When you run above program, you will get below output: 3 You can obviously get differnt outout as we are generating random number here. Generate 10 random integers in range of 1 to 10 console.log("Generating 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) } Generate 10 random integers in range of 1 to 10 7 5 1 10 5 9 7 7 6 2 Generate random number in a range in javascript Here is generic formula to generate random number in a range. function generateRandomInteger(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } // Generate random number between 1 to 10 console.log(generateRandomInteger(1,10)) // Generate random number between 11 to 20 console.log(generateRandomInteger(11,20)) // Generate random number between 21 to 30 console.log(generateRandomInteger(21,30)) 4 17 28 In case, if you don't want to include maximum while generating random numbers, you can use below function. function generateRandomInteger(min, max) { return Math.floor(Math.random() * (max - min)) + min; } That's all about how to generate random number between 1 and 10 in javascript

In this post, we will see how to generate random number between 1 to 10 in javascript.

How to generate random number between 1 and 10 in javascript

We can simply Math.random() method to generate random number between 1 and 10 in javascript.

Math.random() returns a random number between 0(inclusive), and 1(exclusive). That means Math.random() returns always number lower than 1.

We can use Math.random() with Math.floor() to generate random integer.

Here is generic formula to generate random number in the range.

Math.floor(Math.random() * (maximum – minimum + 1)) + minimum
In our case,
minimum = 1
maximum = 10

so it will be
Math.floor(Math.random() * (10 – 1 + 1)) + 1
Math.floor(Math.random() * 10) + 1

So here is the program to generate random number between 1 and 10 in javascript.

When you run above program, you will get below output:

3

You can get different outputs as we are generating random numbers here.

Generate 10 random integers in range of 1 to 10

We can simply use for loop to iterate over the above code to generate 10 random numbers.

Generate 10 random integers in range of 1 to 10
7
5
1
10
5
9
7
7
6
2

Generate random number in a range in javascript

Here is generic formula to generate random number in a range.

4
17
28

In case you don’t want to include maximum while generating random numbers, you can use the below function.

That’s all about how to generate random number between 1 and 10 in javascript.

Was this post helpful?

Related Posts

  • 25 May

    Remove Double Quotes from String in JavaScript

    Table of ContentsWays to Remove Double Quotes from String in JavaScriptUsing replace()Using replaceAll()Using split() and join() methodUsing for loopConclusionWas this post helpful? In this post, we will see how to remove double quotes from String in JavaScript. Ways to Remove Double Quotes from String in JavaScript There are multiple ways to do it. Let’s go […]

  • 25 May

    Convert Seconds to Hours Minutes Seconds in Javascript

    Table of ContentsUsing javascript without any external libraryUsing momentJs libraryWas this post helpful? In this post, we will see how to convert seconds to Hours Minutes Seconds in JavaScript. There are multiple ways to do it. Let’s go through them. Using javascript without any external library To convert seconds to Hours Minutes Seconds in javascript: […]

  • 25 May

    Remove First Character from String in JavaScript

    Table of ContentsWays to Remove First Character from string in JavaScriptUsing substring()Using slice()Using substr()Using replace() methodUsing Array’s shift() with split() and join()ConclusionWas this post helpful? In this post, we will see how to remove first character from string in JavaScript. Since Strings are immutable in JavaScript, you can’t do it in-place. All the methods will […]

  • 25 May

    [Solved] TypeError: toLowerCase is not a function in JavaScript

    TypeError: .toLowerCase is not a function occurs when we call toLowerCase() function on object which is not an string. toLowerCase() function can be only called on string. To resolve this issue, convert value to string using toString() ,method before calling toLowerCase() method. Let’s see with help of simple example [crayon-62dfd6b981b5b537484415/] You will get following error […]

  • 25 May

    Escape Json String Containing Newline Characters in JavaScript

    Table of ContentsEscape JSON string in JavaScriptConclusionWas this post helpful? In this post, we will see how to escape JSON string containing newline characters using JavaScript. There is no well known JavaScript library which can escape all special characters in String. Escape JSON string in JavaScript There is no direct way to escape JSON String […]

  • 23 May

    Escape Apostrophe in JavaScript

    Table of ContentsWays to escape Apostrophe in JavaScriptUsing escape characterUsing alternate String syntaxUsing template literalsWas this post helpful? In this article, we will see how to escape Apostrophe in JavaScript. Let’s see what happens if we have singe quote in String and we also use single quotes to declare the string in JavaScript. [crayon-62dfd6b9823f5064994485/] Output: […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.