SA-MP Forums Archive
frandom() - 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: frandom() (/showthread.php?tid=350061)



frandom() - MP2 - 11.06.2012

I need to get a random float value - can someone help please?

For example I want to be able to do

frandom(2.0)

and get a float between 0 and 2.0, and I don't just mean 0.0 1.0 and 2.0, I mean ANY float, so like 2.4 and 0.4.

If possible I'd also like frandomex(Float:min, Float:max);

Thanks.


Re: frandom() - JhnzRep - 11.06.2012

Uhh maybe

pawn Код:
new frandom = randomEx(0.0000000001, 1.99999999999);



Re: frandom() - Kitten - 11.06.2012

pawn Код:
frandom(Float:max, Float:min = 0.0, dp = 4)
{
    new
        Float:mul = floatpower(10.0, dp),
        imin = floatround(min * mul),
        imax = floatround(max * mul);
    return float(random(imax - imin) + imin) / mul;
}



Re: frandom() - JhnzRep - 11.06.2012

Quote:
Originally Posted by Kitten
Посмотреть сообщение
pawn Код:
frandom(Float:max, Float:min = 0.0, dp = 4)
{
    new
        Float:mul = floatpower(10.0, dp),
        imin = floatround(min * mul),
        imax = floatround(max * mul);
    return float(random(imax - imin) + imin) / mul;
}
Yea, just searched up on ****** and found that from ******..Looks like it should work.


Re: frandom() - Kitten - 11.06.2012

Quote:
Originally Posted by JhnzRep
Посмотреть сообщение
Yea, just searched up on ****** and found that from ******..Looks like it should work.
Yeah, http://forum.sa-mp.com/showpost.php?...52&postcount=3


Re: frandom() - MP2 - 11.06.2012

Thanks!

What's 'dp'?

EDIT: Decimal place I guess.


Re: frandom() - JhnzRep - 11.06.2012

Yes, it is.


Re: frandom() - MP2 - 11.06.2012

Ugh.

pawn Код:
return float(random(imax - imin) + imin) / mul;
warning 213: tag mismatch

Haven't touched it and it doesn't work..?


Re: frandom() - Kitten - 11.06.2012

Make sure your function starts with Float:frandom(etc)


Re: frandom() - MP2 - 11.06.2012

I don't understand - that makes no difference.

What I'm trying to do is add a 'shift' parameter to SetPlayerPos, so players don't pile on top of each-other:

pawn Код:
stock x_SetPlayerPos(playerid, Float:x, Float:y, Float:z, Float:shift=0.0)
{
    if(shift == 0.0) SetPlayerPos(playerid, x, y, z);
    else
    {
   
        x += frandom((0.0-shift), shift);
        y += frandom((0.0-shift), shift);
       
        SetPlayerPos(playerid, x, y, z);
    }
    return 1;
}
So if shift is set to 1, the offset is from -1 to 1.
What do I need to do to fix the error?

EDIT: I fixed it by forwarding it:

pawn Код:
forward Float:frandom(Float:min = 0.0, Float:max, dp = 4);
Float:frandom(Float:min = 0.0, Float:max, 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;
}