Random(X) to pick randomly for specific numbers
#1

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

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

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

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

Or create an array with numbers

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

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


Forum Jump:


Users browsing this thread: 1 Guest(s)