float random - 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: float random (
/showthread.php?tid=655769)
float random -
RedGun2015 - 29.06.2018
Hello, i'm working on a experience level with percent. And i need a float random function, like:
randomFloat(min,max);
to be something like
(random((max) - (min)) + (min)) - this is only for integer but I want for float, I know that random is not for float, but you might now a function to get random float from min and max. ex: randomFloat(30.5, 60.10) to get a random float value from this two..
But I really don't know how to do that.
I've searched on forum, but i didn't find what i need..
Re: float random -
Ultraz - 29.06.2018
You are working on random float spawns?
Re: float random -
RedGun2015 - 29.06.2018
Quote:
Originally Posted by Ultraz
You are working on random float spawns?
|
Nope, exp for next level, ex: you need 253.53 percent for level 5 (you have 53.01).
And I want to give random experience (percent) when doing a job, doing missions or receiving a payday.
Re: float random -
Ultraz - 29.06.2018
I didn't see something like this before.
Check all random codes on sa-mp wiki:
https://sampwiki.blast.hk/wiki/Random
Re: float random -
Calisthenics - 29.06.2018
Why do you bother with floating-point numbers? Experience can be an integer and do your coding much easier.
Anyway, based on your example you can still generate a random number as integer (like the way you showed, it is correct) and then convert to float.
Re: float random -
RedGun2015 - 29.06.2018
Quote:
Originally Posted by ******
Check y_utils, it has one.
|
I checked but I did not find anything of this kind.
Re: float random -
Dayrion - 29.06.2018
Quote:
Originally Posted by RedGun2015
I checked but I did not find anything of this kind.
|
Are you sure?
PHP код:
/*-------------------------------------------------------------------------*//**
* <param name="minmax">Lower bound, or upper bound when only parameter.</param>
* <param name="max">Upper bound.</param>
* <param name="dp">How small to make the differences</param>
* <remarks>
* Generate a random float between the given numbers (min <= n < max). Default
* minimum is 0.0 (changes the parameter order).
* </remarks>
*//*------------------------------------------------------------------------**/
stock Float:RandomFloat(Float:min, Float:max = FLOAT_NAN, dp = 2)
{
new
Float:mul = floatpower(10.0, float(dp));
switch (dp)
{
case 0: mul = 1.0;
case 1: mul = 10.0;
case 2: mul = 100.0;
case 3: mul = 1000.0;
default: mul = floatpower(10.0, float(dp));
}
if (max != max)
{
if (min < 0.0)
return -(float(random(floatround(-min * mul))) / mul);
return float(random(floatround(min * mul))) / mul;
}
// Parameters are the wrong way around - do it anyway.
if (max < min)
return float(random(floatround(min * mul - max * mul))) / mul + max;
// NOT a silly check - "IsNaN".
return float(random(floatround(max * mul - min * mul))) / mul + min;
}