float random
#1

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

You are working on random float spawns?
Reply
#3

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

I didn't see something like this before.
Check all random codes on sa-mp wiki: https://sampwiki.blast.hk/wiki/Random
Reply
#5

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

Quote:
Originally Posted by ******
Посмотреть сообщение
Check y_utils, it has one.
I checked but I did not find anything of this kind.
Reply
#7

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 &lt;= n &lt; max).  Default
 *  minimum is 0.0 (changes the parameter order).
 * </remarks>
 *//*------------------------------------------------------------------------**/
stock Float:RandomFloat(Float:minFloat:max FLOAT_NANdp 2)
{
    new
        
Float:mul floatpower(10.0float(dp));
    switch (
dp)
    {
    case 
0mul 1.0;
    case 
1mul 10.0;
    case 
2mul 100.0;
    case 
3mul 1000.0;
    default: 
mul floatpower(10.0float(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;

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)