Author Topic: Get random float?  (Read 3720 times)

Is there anyway of making a getRandomDecimal?
« Last Edit: June 11, 2014, 04:33:39 AM by Advanced Bot »

getRandom() returns a random normalized float.

getRandom() returns a random normalized float.
Oops, I meant decimal?

What do you mean by "a random decimal"?

Uh, if you mean a random value within a range:

Code: [Select]
function getRandomF(%lim0, %lim1)
{
    %diff = %lim1 - %lim0;
    return getRandom() * %diff + %lim0;
}

Note: The limits don't have to be in a specific order. getRandomF(20, -20); and getRandomF(-20, 20); return values in the same range.

Oops, I meant decimal?
By normalized he means between 0 and 1.

getRandom(0, 1000)/1000

Or similar for however many digits you want?

Code: [Select]
function GetRandomDecimal(%dp)
{
%range = mPow(10,%dp);
%random = getRandom(0,%range) / %range;
return %random;
}
%dp is to how many decimal places.

« Last Edit: June 11, 2014, 04:43:05 PM by Ninjaman 4 »

Uh, if you mean a random value within a range:

Code: [Select]
function getRandomF(%lim0, %lim1)
{
    %diff = %lim1 - %lim0;
    return getRandom() * %diff + %lim0;
}

Note: The limits don't have to be in a specific order. getRandomF(20, -20); and getRandomF(-20, 20); return values in the same range.
Sweet, this is what I was looking for. Thanks.