I'm guessing that most built in random generators return something like this:
[0.0, 1.0)
so if I would like a 50% chance would I use something like this:
if random() < .5
or something like:
if random() <= .5
Thanks for the help.
Why do it yourself? Python has random.choice :)
random.choice([0, 1]) will give you 0/1 with equal chances -- and it is a standard part of Python, coded up by the same people who wrote random.random (and thus know more about its semantics than anyone else)
Ah, the old ".5" problem. Here's the answer:
If you're going to divide 10 things into two equal parts, you need 5 things in each part. 0 thru 4 in the first part, 5-9 in the second part. So... < .5
is correct.
To a first approximation, either works.
The best way is to choose a random generator that specifically emits booleans or integers with range. Then you can nail the range exactly.
Operations like "equality" with floating point is iffy anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With