SA-MP Forums Archive
Setting random amount (Min-Max) - 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: Setting random amount (Min-Max) (/showthread.php?tid=540047)



Setting random amount (Min-Max) - weedxd - 02.10.2014

As you might see im using random number and max is 250. How may i set it to be random from 100-250 ?

PHP код:
public OnPlayerEnterCheckpoint(playerid)
{
    if(
g_var[playerid] == && IsPlayerInRangeOfPoint(playerid5,  2789.6619, -2517.810516.1509))
    {
            
DisablePlayerCheckpoint(playerid);
            new 
vehicleid GetPlayerVehicleID(playerid);
            new 
string[128], amount;
            
amount random (250);
            
format(stringsizeof(string), "*You've earned {FF6347}$%d{33CCFF} from dropping a car!"amount);
            
SendClientMessage(playeridCOLOR_LIGHTBLUEstring);
            
GiveDodMoney(playeridamount);
            
SetVehicleToRespawn(vehicleid);
            
g_var[playerid] = 1;
            
SetTimerEx("CountDown",1200*1000false"d"playerid);
        } 



Re : Setting random amount (Min-Max) - AmirRFCNR - 02.10.2014

define:
pawn Код:
#define MIN_RAND 100
#define MAX_RAND 250
Try to change this:
pawn Код:
amount = random (250);
To:
pawn Код:
amount = random (MIN_RAND,MAX_RAND);



Re: Setting random amount (Min-Max) - weedxd - 02.10.2014

On this line i have the following warn:
Код HTML:
amount = random (MIN_RAND,MAX_RAND);
Код HTML:
warning 202: number of arguments does not match definition
Definition looks like this.
Код HTML:
//Dropcar
new g_var[MAX_PLAYERS];
#define MIN_RAND 100
#define MAX_RAND 250



Re: Re : Setting random amount (Min-Max) - Vince - 02.10.2014

Quote:
Originally Posted by AmirRFCNR
Посмотреть сообщение
...
Are you trying to gain rep or something? Because posting wrong information isn't the way to do it. Do your research first. "Try this" and "Try that" with methods that are obviously wrong (to the trained eye) isn't going to help anyone.

@OP: use this function:
pawn Код:
minrandom(min, max)
    return min + random(max - min);



Re: Setting random amount (Min-Max) - PMH - 02.10.2014

pawn Код:
stock GetRandomBetween(min, max)
{
    new num = random(max);
    if(num > min) return num;
    else if(num < num) return GetRandomBetween(min, max);
}
will work but may take time

edit: Vince got better thing :P


Re: Re : Setting random amount (Min-Max) - weedxd - 02.10.2014

Quote:
Originally Posted by Vince
Посмотреть сообщение
Are you trying to gain rep or something? Because posting wrong information isn't the way to do it. Do your research first. "Try this" and "Try that" with methods that are obviously wrong (to the trained eye) isn't going to help anyone.

@OP: use this function:
pawn Код:
minrandom(min, max)
    return min + random(max - min);
Can you explain me abit more about this min max random amount ?;/


Re: Setting random amount (Min-Max) - weedxd - 02.10.2014

Quote:
Originally Posted by PMH
Посмотреть сообщение
pawn Код:
stock GetRandomBetween(min, max)
{
    new num = random(max);
    if(num > min) return num;
    else if(num < num) return GetRandomBetween(min, max);
}
will work but may take time

edit: Vince got better thing :P
Yeah i noticed but the thing is that Vince didnt explain anything and i have no idea how to use it :/


Re : Re: Setting random amount (Min-Max) - AmirRFCNR - 02.10.2014

Quote:
Originally Posted by weedxd
Посмотреть сообщение
On this line i have the following warn:
Код HTML:
amount = random (MIN_RAND,MAX_RAND);
Код HTML:
warning 202: number of arguments does not match definition
Definition looks like this.
Код HTML:
//Dropcar
new g_var[MAX_PLAYERS];
#define MIN_RAND 100
#define MAX_RAND 250
i have this code too will help you
pawn Код:
stock randomnumber(min, max)
{
    new rand = random(max-min)+min;
    return rand;
}
and change
pawn Код:
amount = randomnumber(MIN_RAND,MAX_RAND);
that working for me


Re: Setting random amount (Min-Max) - Vince - 02.10.2014

Alright, let's suppose that you pass 100 and 250 to the function. What you would end up with (internally) is this:
pawn Код:
return 100 + random(250 - 100)
Which in turn gets simplified to
pawn Код:
return 100 + random(150)
Now, as you know the random function now returns a random value between 0 and 150. Let's suppose the random value is 0. The 100 from before gets added and 100 is returned. Let's suppose the random value is 149. Again, the 100 from before gets added and 249 is returned.

Usage is just the same as the regular random function.

pawn Код:
new randomNumber = minrandom(100, 250);



Re: Setting random amount (Min-Max) - weedxd - 02.10.2014

Issue fixed used my brain abit and found an example in the script.

Код HTML:
amount = 100+random(250);