## WeBWorK Main Forum

### behavior of random()

by Stephen Corwin -
Number of replies: 1

Hi--

The code $x = random(5.1,5.3,0.1) often sets$x to 5.3, but the code $y = random(3.1,3.3,0.1) never seems to set$y to 3.3.  I have tried hundreds of seeds in both WW 2.10 and WW 2.15.  Can anyone explain to me what's going on?

### Re: behavior of random()

by Alex Jordan -

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.