I was watching a VueJS tutorial and was surprised at what it takes in ECMAscript to generate a random integer between 0 and 10.
Math.floor(Math.random() * 10);
In PHP it takes
mt_rand(0, 10);
In your favorite language what does it take to generate an integer between 0 and 10?
Oldest comments (48)
From a terminal can be(no for encrypt things):
They see me shuffling. LMFAO
Python:
Ruby:
Go:
None of these are cryptographically secure though
Python has a cryptographically secure library called "secrets", which I use a lot :-P
Yeah secrets is very useful and has a clear API
JS still works better than C to randomly generate and have the numbers actually be random π
Math.random()
produces a randomfloat
(all JavaScript numbers are of typefloat
) from0
to1
.Adding the requirement of 0-10 means you have to multiply by 10 or
* 10
.The last requirement was the number must be an
Integer
, which is themath.floor
.Of course, if you only wanted a random float between
0
and1
, it would be just one call.And in PHP if you wanted a random float between 0 and 1, you would also have to do more work: Random Float between 0 and 1 in PHP
So the complexity actually comes from the requirements you give it.
But asking for a random integer is a common task. So it's handy to create function and keep it in your library. Don't go sprinkling
Math.floor(Math.random() * 10)
randomly around your codebase :DP.S. I prefixed this with
pseudo
because there is no true Random in JavaScript. This is very important when it comes to cryptography. GooglePRNG
if you want to learn more.For a cryptographically strong random value, you might want to look into Crypto.getRandomValues()
Cheers!
I wish PM's understood this more.
There's a bug in your example; if the output is supposed to be
[start, end)
, then the correct implementation is:βπ
I probably should have run it once. lol.
I gotta stop typing code directly into editors.
Good catch!
Going the OG route
const random = 7;
π€£
xkcd.com/221/
You made my day π€£π€£
Pfff, everyone knows
random
is4
not7
.Pfff, you need update your mindset π€¨
Updates just break everything.
How can you know that no consumer of your code assumes it will be
4
?Sure, you claimed it is random, in the "documentation", but it's not like that ever works.
There are probably children out there holding down spacebar to stay warm in the winter! YOUR UPDATE MURDERS CHILDREN.
Python has a few options, including the numpy module. Regardless of language, however, it often only takes a ~20 lines to define a pseudo-random (in numerical math we avoid saying "random") number generator method. Here is an implementation of the so-called Mersenne twister:
In this method, we declare two integers, i (16807 = 75), and j (the Mersenne prime). In specifying the arguments n (number of psuedo-random numbers desired), seed (for repeatability of the pseudo-random numbers), and S (min, max, types to be generated), what is repeatedly happening (depending on n) is that we are taking the remainder of (seed * i) and j, and dividing by j; over and over. This implementation of the Mersenne Twister is good for about 230 random numbers before it repeats itself. :)
Others responded on-topic, so I would add an off-topic :)
I do not build a JS project without Lodash (or something similar), it covers the basic needs and lacks of JS, acting like a Standard library in other languages. A few of them are related to random
Devs should be more careful at the distributions, most of the cases the code will be used for a few values and a small range of values, which will lead to very not-so-random results.
If the random results will affect the User Experience you should think twice of using it, the screwed distribution will affect your sales and KPIs in unknown ways, most likely negative, and there are always better alternatives, that require more resources to implement of course.
real(8) :: r
integer :: i
CALL RANDOM_NUMBER(r)
i = floor(r *10.0)
I originally found it on the Hey, Scripting Guy! blog, but it's pretty simple to use:
Where
Count
is how many random numbers you wish to generate, andInputObject
is an array of the range of numbers you want to choose from.