In Javascript, Math.random() produces a float value between 0 and 1, so I multiply by 1000 and round it to get a larger integer. The value %2 == 0 is a non-library way of performing isEven() on the random integer (value % 2 is 0 if even, 1 if odd, and the ==0 makes it return a boolean). When used in the if statement, it’s essentially a coin flip.
does javascript not allow you to interpret integers as booleans in a conditions directly? seems it’d be simpler to just do math.round(math.random()), which should still get you true (1) or false (0) in equal likelihood. or am i missing something?
In Javascript,
Math.random()
produces a float value between 0 and 1, so I multiply by 1000 and round it to get a larger integer. Thevalue %2 == 0
is a non-library way of performingisEven()
on the random integer (value % 2
is 0 if even, 1 if odd, and the==0
makes it return a boolean). When used in theif
statement, it’s essentially a coin flip.does javascript not allow you to interpret integers as booleans in a conditions directly? seems it’d be simpler to just do math.round(math.random()), which should still get you true (1) or false (0) in equal likelihood. or am i missing something?
It’ll give you 1 ~= true or 0 ~= undefined, but I typically use Typescript which prefers actual booleans to boolean-ish
huh. interesting. i wonder what number it’s actually storing for false then?