WeBWorK Main Forum

behavior of random()

Re: behavior of random()

by Alex Jordan -
Number of replies: 0

Likely it is machine rounding with floating point reals. I have not checked the details, but I would guess
5.1 + 2*0.1
comes out slightly less (or equal to) 5.3. So (5.1, 5.2, 5.3) are all considered. (Or at least, numbers very close to them are considered.)

But my guess is
3.1 + 2*0.1
comes out like 3.3000000000001

and then it's greater than the upper bound of 3.3 and excluded from consideration.

If this explains it, perhaps the random() routine should add a tiny tolerance to the upper bound as a way to get around this. For something urgent, you could use
random(51,53,1)/10
to just work with integers.