Author Topic: [RESOURCE] getRandomFloat  (Read 1013 times)

Figured some people may find use for this even though it took probably ten seconds to code.

Unsure of there's a reason I've never seen people do this, could just be because nobody needs it.

Code: [Select]
function getRandomFloat(%min, %max, %len)
{
%len = mAbs(%len);
if(%len > 5)
%len = 5;
%power = mPow(10, (%len $= "" ? 0 : %len));
%min *= %power;
%max *= %power;
return getRandom(%min, %max) / %power;
}

Usage:
getRandomFloat(%min, %max[, %len]);
Returns a random float between %min and %max, with a maximum float length of %len (Defaults to zero, maximum is five).

this is exactly what getRandom(); does.

this is exactly what getRandom(); does.
getrandom with a min and max only returns integers

with no arguments it's 0.0-1.0

with one argument, it's an integer 0-max
« Last Edit: February 24, 2013, 12:31:27 AM by otto-san »

getrandom with a min and max only returns integers

with no arguments is it not 0-1
with no arguments it is a float.

with no arguments it is a float.
yes.

however then it's only zero to one.



why so overcomplicated

Code: [Select]
function getRandomFloat( %min, %max )
{
return %min + getRandom() * ( %max - %min );
}

and if you want fixed length

mFloatLength( getRandomFloat( min, max ), len )