5 ms·
Probably random() returns a value from 0 to one, multiplying by 6 yields a random point between 0 and 6. There is a 1 in 6 chance for this number to be in range
by jesprenj 4y ago
Probably random() returns a value from 0 to one, multiplying by 6 yields a random point between 0 and 6. There is a 1 in 6 chance for this number to be in range [0, 1), which is checked by rounding it down and checking if it's zero.
right?
- pluc 4y ago> The Math.random() static method returns a floating-point, pseudo-random number that's greater than or equal to 0 and less than 1 What a poor name for a method.
- kolinko 4y agowhy?
- pluc 4y agoIt just doesn't feel natural. If I ask you to give me a random number, are you going to assume it's random among all existing numbers or between the two smallest ones? I'd do Math.random(min,max) and then it could default to 0,1 though I guess you could go Math.random()*100 or whatever.. guess it just doesn't feel like good design or very convenient/readable - but then again this is JavaScript we're talking about.
- quesera 4y ago> I'd do Math.random(min,max) But then you'd need a different way to do random non-integers. Randomness does not naturally produce an integer result. The 0-1 range with precision determined by the architecture is actually the simplest and most logical and flexible way to do it. [EDIT: floats are never simple, see below!] Some languages offer convenience functions on top of the low level random number generator. I don't know what's available in JavaScript. E.g. in ruby: irb(main):001:0> rand => 0.5562701792527804 irb(main):002:0> rand(100) => 44 irb(main):003:0> rand(44..203) => 188 ...but of course, I could go on all day long about how pleasant Ruby is to work with. :)
- deathanatos 4y ago> Randomness does not naturally produce an integer result. The 0-1 range with precision determined by the architecture is actually the simplest and most logical and flexible way to do it. The most trivial way, I would think, would be to interpret a stream of bits would be as an unsigned integer. E.g., a u32 is just "take 32 bits of random data from your RNG, and cast." That's certainly far more natural than trying to build an IEEE double from [0, 1) with a uniform distribution. I'm actually not sure how you'd do that without doing something like starting with a random u32 and dividing by (U32_MAX + 1). Like maybe you can shove the RNG output into the mantissa, but it is not at all obvious to me about the correctness of that.
- quesera 4y agoMmm, great points. At least for range [0,MAXINT], it is simple to fill the bitspace for an integer. Range [0,limit) requires slightly more-awkward division. The problem of uniformity across the range in a populated float is critical -- as you point out, IEEE float layouts are not simple. I would guess that the "float" result is constructed as a fixed-point decimal in the same way you would an integer (sprinkle the RNG bits over the storage bitspace), but returned as float (via cast or math) to match the language type system.
- pama 4y agoI would have agreed 50 years ago. It has been used in many libraries in many languages historically, nowadays it would be confusing if it returned anything else.
- messe 4y agoSeems like a perfectly reasonable range, and what I'd expect for a method named that. If it was uniformly distributed over the range of a double, it would almost always be of large magnitude, which wouldn't be particularly useful.
- alpaca128 4y agoEvery random generator API I've seen uses this interval by default. And it makes sense, what other range is useful in more situations and platform independent? In practically every other case you'd want to specify your desired range with arguments.
- sgerenser 4y agoThe classic C rand() function returns an integer between 0 and RAND_MAX. Not being much of a JS developer I would have expected something more like that.
- numpad0 4y ago"Math.random() generates a randomized binary sequence, which is cast and returned as a floating point type; sign and exponent bits are hard-coded, such that the sequence may represent a number between 0.0f and 1.0f. NOTE: This method should NOT be used for cryptographic and/or security use cases. Implementation is performance oriented and is generally recommended for all non-sensitive programming, such as user interactive visualizations, and randomized suggestions." Better?
- deleted 4y ago[deleted]