Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random numbers and floor vs round function

Tags:

javascript

Why if I use random number generator and range 0 - 9 I don't get the same uniform distribution as combined it with the floor function?

like image 756
user336635 Avatar asked Sep 11 '11 10:09

user336635


People also ask

What is the difference between Math floor and Math round?

Math. round() - rounds to the nearest integer (if the fraction is 0.5 or greater - rounds up) Math. floor() - rounds down.

What is the difference between Math round () and Math Ceil ()?

ceil( ) differs from Math. round( ) in that it always rounds up, rather than rounding up or down to the closest integer. Also note that Math. ceil( ) does not round negative numbers to larger negative numbers; it rounds them up toward zero.

How do you use Math random and Math floor together?

random generates a number between 0 and 1, that isn't a whole number, and also isn't 1. To get a number, for example between 0 and 10, multiply your answer by 10: Math. random() * 10 To get it to be a whole number, i.e. an integer, apply Math. floor, which rounds down to the nearest whole number: Math.


2 Answers

I really like to trigger 31-bit int instead of Math.floor(), by doing a binary "or 0" operation. It makes it slightly faster (stack vs heap,) seems a tad neater, and does the same thing, in a practical sense. Here is an example that gets a random element of an array:

var ar=[1,2,3,4,5,6,7,8,9,0,'a','b','c','d']
console.log(ar[(Math.random() * ar.length) |0])

This might seem like premature optimization, but as I said, I like how it looks, and it takes less typing than Math.floor(). Using Math.round() would require an extra step (because the spread is 1 to ar.length not 0 to ar.length-1)

like image 29
konsumer Avatar answered Oct 05 '22 18:10

konsumer


Math.floor(Math.random() * 10) gives quite uniform distribution, while Math.round(Math.random() * 10) doesn't.

Math.floor() returns 0 for any value in the range [0, 1) (1 exclusive), 1 for any value in the range [1, 2), etc.

So if we have equal chances of getting a number in one of these ranges, we will get an equal distribution of 0's and 1's.

Math.round(), however, returns 0 for values under 0.5, 1 for values under 1.5, etc.

So we actually have half the chances of getting a 0, as only values from 0 to 0.5 will round to 0.

╔═════════╦═════════╦═════════╗
║  Range  ║ floor() ║ round() ║
╠═════════╬═════════╬═════════╣
║ [0, 1)  ║ 0       ║ 0 or 1  ║
║ [1, 2)  ║ 1       ║ 1 or 2  ║
║ [2, 3)  ║ 2       ║ 2 or 3  ║
║ ...     ║ ...     ║ ...     ║
║ [9, 10) ║ 9       ║ 9 or 10 ║
╚═════════╩═════════╩═════════╝
like image 175
Arnaud Le Blanc Avatar answered Oct 05 '22 18:10

Arnaud Le Blanc