jeudi 17 décembre 2015

JavaScript: How to interpret a variation of the `Math.floor(Math.random)` method?

It seems that Math.random()*3|0 equates to Math.floor(Math.random() * 3), which is the version I'm familiar with. While I have no problem understanding the step-by-step process of how the latter generates integers 0, 1, and 2, the structure of the former stumps me. It may well be an idiomatic variation of the more roundabout Math.floor method.

Is it possible to express the following two snippets of code in x|y style instead and produce the same results? If so, could you break down how it works?


1 + Math.floor(Math.random() * 100) // yielding 1-100 (inclusive)

190 + Math.floor(Math.random() * 66) // yielding 190-255 (inclusive)




Aucun commentaire:

Enregistrer un commentaire