Generating random whole numbers in JavaScript in a specific range?

zacharyliu

How can I generate random whole numbers between two specified variables in JavaScript, e.g. x = 4 and y = 8 would output any of 4, 5, 6, 7, 8?

Ionuț G. Stan

There are some examples on the Mozilla Developer Network page:

/**
 * Returns a random number between min (inclusive) and max (exclusive)
 */
function getRandomArbitrary(min, max) {
    return Math.random() * (max - min) + min;
}

/**
 * Returns a random integer between min (inclusive) and max (inclusive).
 * The value is no lower than min (or the next integer greater than min
 * if min isn't an integer) and no greater than max (or the next integer
 * lower than max if max isn't an integer).
 * Using Math.round() will give you a non-uniform distribution!
 */
function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

Here's the logic behind it. It's a simple rule of three:

Math.random() returns a Number between 0 (inclusive) and 1 (exclusive). So we have an interval like this:

[0 .................................... 1)

Now, we'd like a number between min (inclusive) and max (exclusive):

[0 .................................... 1)
[min .................................. max)

We can use the Math.random to get the correspondent in the [min, max) interval. But, first we should factor a little bit the problem by subtracting min from the second interval:

[0 .................................... 1)
[min - min ............................ max - min)

This gives:

[0 .................................... 1)
[0 .................................... max - min)

We may now apply Math.random and then calculate the correspondent. Let's choose a random number:

                Math.random()
                    |
[0 .................................... 1)
[0 .................................... max - min)
                    |
                    x (what we need)

So, in order to find x, we would do:

x = Math.random() * (max - min);

Don't forget to add min back, so that we get a number in the [min, max) interval:

x = Math.random() * (max - min) + min;

That was the first function from MDN. The second one, returns an integer between min and max, both inclusive.

Now for getting integers, you could use round, ceil or floor.

You could use Math.round(Math.random() * (max - min)) + min, this however gives a non-even distribution. Both, min and max only have approximately half the chance to roll:

min...min+0.5...min+1...min+1.5   ...    max-0.5....max
└───┬───┘└────────┬───────┘└───── ... ─────┘└───┬──┘   ← Math.round()
   min          min+1                          max

With max excluded from the interval, it has an even less chance to roll than min.

With Math.floor(Math.random() * (max - min +1)) + min you have a perfectly even distribution.

min.... min+1... min+2 ... max-1... max.... max+1 (is excluded from interval)
|        |        |         |        |        |
└───┬───┘└───┬───┘└─── ... ┘└───┬───┘└───┬───┘   ← Math.floor()
   min     min+1               max-1    max

You can't use ceil() and -1 in that equation because max now had a slightly less chance to roll, but you can roll the (unwanted) min-1 result too.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Generating decimal random numbers in Java in a specific range?

From Dev

Generating random numbers in specific binary range

From Dev

Generating random evenly divisible numbers within a specific range in C++?

From Dev

Generating random numbers in a particular range

From Dev

Generating random numbers in a specific interval

From Dev

Generating random numbers in a specific interval

From Dev

Generating random numbers over a range in Go

From Dev

Generating small range of different random numbers?

From Dev

Generating and storing unique and random numbers within a range

From Dev

Generate random numbers in specific range

From Dev

Generating random date in a specific range in JAVA

From Dev

how to generate unique random numbers with a specific range

From Dev

how to generate unique random numbers with a specific range

From Dev

C++11 Generating random numbers from frequently changing range

From Dev

Generating uniformly distributed random numbers within a range in C++

From Dev

Generating Random numbers and printing "Done!" if they fall within a range in C

From Dev

Generating random numbers (0 and 1) given specific probability values in R

From Java

Generating random numbers with Swift

From Dev

Generating random numbers in Haskell

From Dev

Generating unique random numbers

From Dev

Generating weighted random numbers

From Dev

Generating random poll numbers

From Dev

Generating groups of random numbers

From Dev

Generating unique random numbers

From Dev

Generating a set of random numbers

From Dev

Generating sequence of random numbers

From Dev

Generating random numbers randomly

From Dev

How to random numbers in the range -0.8 to 0.8 in javascript?

From Dev

%random% in batch and how to avoid specific numbers within a specific range?

Related Related

  1. 1

    Generating decimal random numbers in Java in a specific range?

  2. 2

    Generating random numbers in specific binary range

  3. 3

    Generating random evenly divisible numbers within a specific range in C++?

  4. 4

    Generating random numbers in a particular range

  5. 5

    Generating random numbers in a specific interval

  6. 6

    Generating random numbers in a specific interval

  7. 7

    Generating random numbers over a range in Go

  8. 8

    Generating small range of different random numbers?

  9. 9

    Generating and storing unique and random numbers within a range

  10. 10

    Generate random numbers in specific range

  11. 11

    Generating random date in a specific range in JAVA

  12. 12

    how to generate unique random numbers with a specific range

  13. 13

    how to generate unique random numbers with a specific range

  14. 14

    C++11 Generating random numbers from frequently changing range

  15. 15

    Generating uniformly distributed random numbers within a range in C++

  16. 16

    Generating Random numbers and printing "Done!" if they fall within a range in C

  17. 17

    Generating random numbers (0 and 1) given specific probability values in R

  18. 18

    Generating random numbers with Swift

  19. 19

    Generating random numbers in Haskell

  20. 20

    Generating unique random numbers

  21. 21

    Generating weighted random numbers

  22. 22

    Generating random poll numbers

  23. 23

    Generating groups of random numbers

  24. 24

    Generating unique random numbers

  25. 25

    Generating a set of random numbers

  26. 26

    Generating sequence of random numbers

  27. 27

    Generating random numbers randomly

  28. 28

    How to random numbers in the range -0.8 to 0.8 in javascript?

  29. 29

    %random% in batch and how to avoid specific numbers within a specific range?

HotTag

Archive