Getting wrong return on Rand()

BlitzMax Forums/BlitzMax Programming/Getting wrong return on Rand()

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

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.

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