SA-MP Forums Archive
random() but as a float? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: random() but as a float? (/showthread.php?tid=240128)



random() but as a float? - Celson - 15.03.2011

Is there a function out there that creates a random number with decimals?


Re: random() but as a float? - pawn_ - 15.03.2011

pawn Code:
float(random(...))



Re: random() but as a float? - wups - 15.03.2011

What is Dp? Is it the number count after the dot?


Re: random() but as a float? - jamesbond007 - 24.12.2011

Quote:
Originally Posted by ******
View Post
pawn Code:
frandom(Float:max, Float:min = 0.0, dp = 4)
{
    new
        // Get the multiplication for storing fractional parts.
        Float:mul = floatpower(10.0, dp),
        // Get the max and min as integers, with extra dp.
        imin = floatround(min * mul),
        imax = floatround(max * mul);
    // Get a random int between two bounds and convert it to a float.
    return float(random(imax - imin) + imin) / mul;
}
You can use that as:

pawn Code:
new Float:rand = frandom(5.5); // Any float from 0.0 to 5.5 (up to 4 dp).
new Float:rand = frandom(5.5, -5.5); // Any float from -5.5 to 5.5 (up to 4 dp).
new Float:rand = frandom(5.5, 0.0, 1); // Any float from 0.0 to 5.5 (up to 1 dp).
new Float:rand = frandom(5.5, -5.5, 1); // Any float from -5.5 to 5.5 (up to 1 dp).
i get tag mismatch on this line
return float(random(imax - imin) + imin) / mul;
:/


Re: random() but as a float? - Simon - 24.12.2011

Try changing the header of the method from:

frandom(Float:max, Float:min = 0.0, dp = 4)

to:

Float:frandom(Float:max, Float:min = 0.0, dp = 4)