this post was submitted on 05 Dec 2024
235 points (93.7% liked)

Science Memes

11408 readers
1046 users here now

Welcome to c/science_memes @ Mander.xyz!

A place for majestic STEMLORD peacocking, as well as memes about the realities of working in a lab.



Rules

  1. Don't throw mud. Behave like an intellectual and remember the human.
  2. Keep it rooted (on topic).
  3. No spam.
  4. Infographics welcome, get schooled.

This is a science community. We use the Dawkins definition of meme.



Research Committee

Other Mander Communities

Science and Research

Biology and Life Sciences

Physical Sciences

Humanities and Social Sciences

Practical and Applied Sciences

Memes

Miscellaneous

founded 2 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 3 points 2 weeks ago* (last edited 2 weeks ago) (1 children)

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.

[–] [email protected] 3 points 1 week ago (1 children)

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?

[–] [email protected] 1 points 1 week ago* (last edited 1 week ago) (1 children)

It'll give you 1 ~= true or 0 ~= undefined, but I typically use Typescript which prefers actual booleans to boolean-ish

[–] [email protected] 1 points 1 week ago

huh. interesting. i wonder what number it's actually storing for false then?