Getting wrong return on Rand()
BlitzMax Forums/BlitzMax Programming/Getting wrong return on Rand() You're saying integers between -1 and 1 which is 0 and 1.
To include -1 you need rand(-2,1)
<Edit> Why is it throwing off your probabilities?
<edit> In fact, this might be caused by the 'double' precision issue.
To include -1 you need rand(-2,1)
<Edit> Why is it throwing off your probabilities?
<edit> In fact, this might be caused by the 'double' precision issue.
Rand() is for integers.
but uses doubles.
Do you want a random float? If so try RndFloat() which returns a number from 0.0 to 1.0
Because RAND returns a double but the function is declared to return an int, values such as -0.98 are set
to 0.
It is possible to get -1 returned but not 1/3 of the time.
Setting rand(-2,1) will return values such as -1.9 set to
-1.
<edit> B3D doesn't have this problem.
to 0.
It is possible to get -1 returned but not 1/3 of the time.
Setting rand(-2,1) will return values such as -1.9 set to
-1.
<edit> B3D doesn't have this problem.
Matt...Rnd() is for floats. I'm looking for an integer hence I'm using Rand().
When I say throwing off my probabilities I mean I'll get 0 more than -1 or 1. As you said there's not an even 1/3 chance to get each number by using Rand(-2,1) because anything in the -1 range is coming up a zero.
It would actually be 1 1/4 of the time, 0 1/2 of the time, and -1 1/4 of the time.
Hope this gets fixed soon.
When I say throwing off my probabilities I mean I'll get 0 more than -1 or 1. As you said there's not an even 1/3 chance to get each number by using Rand(-2,1) because anything in the -1 range is coming up a zero.
It would actually be 1 1/4 of the time, 0 1/2 of the time, and -1 1/4 of the time.
Hope this gets fixed soon.
The Rand(0,2) - 1 trick is good enough for this particular problem.
The trouble is that Rand() should round down, i.e. toward -Infinity.
It actually rounds toward zero, which is how BlitzMax converts floating point values to integer.
Here is an alternate version of Rand() which uses Floor() to enforce the correct rounding.
I haven't seriously tested this, but it seems like it should work.
The trouble is that Rand() should round down, i.e. toward -Infinity.
It actually rounds toward zero, which is how BlitzMax converts floating point values to integer.
Here is an alternate version of Rand() which uses Floor() to enforce the correct rounding.
I haven't seriously tested this, but it seems like it should work.
Function Rand2( min_value,max_value=1 ) Local delta!=Double(max_value)-Double(min_value) If delta>0 Return Floor( RndDouble()*(1+delta) ) + min_value Return Floor ( RndDouble()*(1-delta) ) + max_value End Function
Beaker for the win!
Hope this gets fixed soon.
Me too!