frandom()
#1

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.
Reply
#2

Uhh maybe

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

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;
}
Reply
#4

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.
Reply
#5

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
Reply
#6

Thanks!

What's 'dp'?

EDIT: Decimal place I guess.
Reply
#7

Yes, it is.
Reply
#8

Ugh.

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

Haven't touched it and it doesn't work..?
Reply
#9

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

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;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)