Top community members

Have you ever heard about encyclopedia for code?

What about: Wiki for Code project?

* because I've tried to share code and my solutions on different services and it was removed or I was unwelcomed
   - maybe it wasn't good place, but this one is.

if you think you have solution that can help someone, you are welcome to share this solution - Click here

JavaScript - random number between MIN and MAX value - both inclusive

3 points
Asked by:
Ticket Ninja
2338

How to generate random number in JavaScript between min and max value?
The condition - both MIN and MAX value have to be inclusive.
[0, 3] - both inclusive (where 0 is min and 3 is max).

1 answers
1 points
Answered by:
Ticket Ninja
2338

Quick solution with usage example:

// ONLINE-RUNNER:browser;

// [min, max]
// inclusive min
// inclusive max
function randomNumber(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
}

for (var i = 0; i < 15; i++) {
    var randomNum = randomNumber(0, 3);
    console.log(randomNum);
}

Output (note that we use Random so the output each time will be different :D, or there is high probability it will be):

3
0
3
3
1
1
1
0
1
3
1
2
1
3
3

 

0 comments Add comment

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more