SA-MP Forums Archive
How can I make random with 'real' numbers? - 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: How can I make random with 'real' numbers? (/showthread.php?tid=318985)



How can I make random with 'real' numbers? - nuriel8833 - 17.02.2012

How can I make a random with 'real' numbers (=not integers)
for ex:
pawn Код:
RandomEx(2264.60,2267.20); // From a stock

stock RandomEx(min,max) { return random(max-min)+min; }
^^
The above gives me the 'tag mismatch' warning
-----------------------
Thanks 4 helpers


Re: How can I make random with 'real' numbers? - KingHual - 17.02.2012

The native random doesn't accept floats. You just mindfucked me, but I figured it out, anyway I scripted this, and it worked like a charm
Код:
stock Float:RandomEx(Float:max)
{
new Float:range = max;
new Float:num = range * random(32767)/32767;
return num;
}
And if you want minimum value param, try this:
Код:
stock Float:RandomEx(Float:min,Float:max)
{
new Float:range = max-min;
new Float:num = min+range * random(32767)/32767;
return num;
}



Re: How can I make random with 'real' numbers? - KingHual - 17.02.2012

Quote:
Originally Posted by ******
Посмотреть сообщение
What's with all the extra variables?

pawn Код:
stock Float:frandom(Float:max)
{
    return max * random(32768) / 32768.0;
}
pawn Код:
stock Float:frandomex(Float:min, Float:max)
{
    return min + (max - min) * random(32768) / 32768.0;
}
Trust me, reduction like this is good in PAWN (as is the ".0") and the use of "32768" instead of "32767".
Mm'kay, though I only gave him the example function. And why is "32768" better than "32767" ? Because it's even?


Re: How can I make random with 'real' numbers? - KingHual - 17.02.2012

Quote:
Originally Posted by ******
Посмотреть сообщение
Power of two.
Ah, yes, didn't even go through my mind lol


Re: How can I make random with 'real' numbers? - nuriel8833 - 18.02.2012

Quote:
Originally Posted by ******
Посмотреть сообщение
What's with all the extra variables?

pawn Код:
stock Float:frandom(Float:max)
{
    return max * random(32768) / 32768.0;
}
pawn Код:
stock Float:frandomex(Float:min, Float:max)
{
    return min + (max - min) * random(32768) / 32768.0;
}
Trust me, reduction like this is good in PAWN (as is the ".0") and the use of "32768" instead of "32767".
What do I need to put instead of the Float:frandomex?


Re: How can I make random with 'real' numbers? - KingHual - 18.02.2012

Quote:
Originally Posted by nuriel8833
Посмотреть сообщение
What do I need to put instead of the Float:frandomex?
That's your stock's name. Don't remove the float, but you can change the part after the Float: to whatever you like.


Re: How can I make random with 'real' numbers? - nuriel8833 - 18.02.2012

Quote:
Originally Posted by king_hual
Посмотреть сообщение
That's your stock's name. Don't remove the float, but you can change the part after the Float: to whatever you like.
Alright,thanks a lot both!


Re: How can I make random with 'real' numbers? - nuriel8833 - 18.02.2012

Sorry for doublepost,but theres a warning:
pawn Код:
warning 208: function with tag result used before definition, forcing reparse
Why?


Re: How can I make random with 'real' numbers? - KingHual - 18.02.2012

Quote:
Originally Posted by nuriel8833
Посмотреть сообщение
Sorry for doublepost,but theres a warning:
pawn Код:
warning 208: function with tag result used before definition, forcing reparse
Why?
Well, not sure, but what did you change in the code? Because it worked fine for me.


Re: How can I make random with 'real' numbers? - nuriel8833 - 18.02.2012

Quote:
Originally Posted by king_hual
Посмотреть сообщение
Well, not sure, but what did you change in the code? Because it worked fine for me.
All I did is this:
pawn Код:
stock Float:RandomEx(Float:min, Float:max) { return min + (max - min) * random(32768) / 32768.0; }