SA-MP Forums Archive
Question - How to make random based on percentage - 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: Question - How to make random based on percentage (/showthread.php?tid=546874)



Question - How to make random based on percentage - buburuzu19 - 18.11.2014

How to make random function by percentage ( i mean chance)
if i have a command like: /random , the change to show up the message "Winner" is 1%


Re: Question - How to make random based on percentage - Write - 18.11.2014

Pretty basic.

pawn Код:
new rand = random(3);
switch(rand)
{
    case 0:
    {
       SendClientMessage(playerid, -1, "You are a winner!");
    }
    case 1:
    {
        SendClientMessage(playerid, -1, "You are a loser!");
    }
    case 2:
    {
        SendClientMessage(playerid, -1, "You are a loser!");
    }
You can change the value of random times that you desire by changing "new rand = random(3);" to maybe 4 or 5 and then put new cases at the bottom.


Re: Question - How to make random based on percentage - buburuzu19 - 18.11.2014

I asked how to add percentage to random function.
I have a cmd named /getprize and i want to make the cases with percentage cuz i put some really really big prizes and for example the nrg can be won at a percentage of 1%.
That is what i am needing.


Re: Question - How to make random based on percentage - Write - 18.11.2014

And that's exactly what I showed you up there, unless you want me to make a '/getprize' command for you entirely, in which I won't do.

You can however seek for such a request here; https://sampforum.blast.hk/showthread.php?tid=447813.


Re: Question - How to make random based on percentage - buburuzu19 - 18.11.2014

https://sampforum.blast.hk/showthread.php?tid=388165
Something like this. Read the full topic.


Re: Question - How to make random based on percentage - oliverrud - 18.11.2014

Then you just change his code a little bit?
pawn Код:
new rand = random(100);
switch(rand)
{
    case 1:
    {
       SendClientMessage(playerid, -1, "You are a winner!");
    }
    default:
    {
        SendClientMessage(playerid, -1, "You are a loser!");
    }
}



Re: Question - How to make random based on percentage - buburuzu19 - 18.11.2014

For example:
New randprize = random(3);
Switch(random)
{
Case: 0
{
SCM..."You won a NRG"
}
Case 1:
{
SCM..."You won 500$"
}
Case 2:
{
SCM..."You won 300$"
}
How to make case 0 to be hard to win.


Re: Question - How to make random based on percentage - CutX - 18.11.2014

//EDIT: oliverrud posted the correct solution already, didnt see it >.<
the correct way to do it.

1% chance to win, if it's 99 (100-1, cuz max of random is value-1) you'll win
pawn Код:
new rand = random(100);
switch(rand)
{
    case 0..98://simply means >= 0 and  <=98
    {
       SendClientMessage(playerid, -1, "You are a loser!");
    }
    default://the rest (99)
    {
        SendClientMessage(playerid, -1, "You are a winner!");
    }
}



Re: Question - How to make random based on percentage - oliverrud - 18.11.2014

Quote:
Originally Posted by buburuzu19
Посмотреть сообщение
For example:
New randprize = random(3);
Switch(random)
{
Case: 0
{
SCM..."You won a NRG"
}
Case 1:
{
SCM..."You won 500$"
}
Case 2:
{
SCM..."You won 300$"
}
How to make case 0 to be hard to win.
Simple solution off the top of my head would be something like this:
pawn Код:
New randprize = random(100);
Switch(random)
{
 Case: 0:
 {
     SCM..."You won a NRG"
}
Case 1..15:
{
 SCM..."You won 500$"
}
Case 16..40:
{
  SCM..."You won 300$"
}
default:{
//yalost
}



Re: Question - How to make random based on percentage - buburuzu19 - 18.11.2014

And how to do if i have 2 prizes rare to win?