SA-MP Forums Archive
Random(X) to pick randomly for specific 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: Random(X) to pick randomly for specific numbers (/showthread.php?tid=560263)



Random(X) to pick randomly for specific numbers - CalvinC - 27.01.2015

You can use a switch like this to get a random number:
pawn Код:
switch(random(3))
{
    case 1: // Codes
    case 2: // Codes
    case 3: // Codes
}
I know that, but how could you create a feature.
If i only want 1 and 3, i could probably do this:
pawn Код:
switch(random(3))
{
    case 1: // Codes
    case 2:
    {
        switch(random(2))
        {
            case 1: // Codes from previous case 1
            case 2: // Codes from previous case 3
        }
    }
    case 3: // Codes
}
But that would become very messy if you have alot of specific numbers like me.


Re: Random(X) to pick randomly for specific numbers - Sime30 - 27.01.2015

Why are you complicating if I may ask? For what do you need this "feature"?


Re: Random(X) to pick randomly for specific numbers - Jefff - 27.01.2015

random starts from 0 if you want 1,2 use random(2)+1
pawn Код:
switch(random(2)+1)
{
    case 1: // code
    default: or case 2: // code
}



Re: Random(X) to pick randomly for specific numbers - CalvinC - 27.01.2015

Quote:
Originally Posted by Sime30
Посмотреть сообщение
Why are you complicating if I may ask? For what do you need this "feature"?
I just thought about what you said actually, and it turns out that i was quite stupid... So yeah i fixed it, don't even need this at all.

EDIT: Thanks Jefff, i just made it quickly so i didn't realise, but it was just an example anyways.


Re: Random(X) to pick randomly for specific numbers - ball - 27.01.2015

Or create an array with numbers

Код:
new theArray[] = {1, 3}, theRandom = random(sizeof theArray);

switch(theArray[theRandom])
{
	case 1: ...
	case 3: ...
}