Picking a random number from a list
#1

Hello guys,
how do you make a code that picks a random number from your chosen list? So we have a list of let's say 1, 12, 14 and 25. How do I make it so it randomly picks one of those numbers?
Reply
#2

store the numbers in an array.
count the numbers in the array.
generate random number from the total numbers in the array.
select the random number from the array index.
Reply
#3

Quote:
Originally Posted by cessil
Посмотреть сообщение
store the numbers in an array.
count the numbers in the array.
generate random number from the total numbers in the array.
select the random number from the array index.
/sarcasm Thankyou for telling me exactly how to do it.
Reply
#4

The way I did this, like if you had to pick a random player id, was to store all of the possible choices in an array and then pick randomly from the array size. Maybe like this.

pawn Код:
new playerlist[MAX_PLAYERS],
    size;  // Maybe this is uneccessary and you could use sizeof

for(new i; i<MAX_PLAYERS; i++)
{
    if(IsPlayerConnected(i))
    {
        playerlist[size] = i;
        size++;
    }
}

new rand = playerlist[random(size) + 1];
Something like that I think.

Edit : Too slow...
Reply
#5

Quote:
Originally Posted by Backwardsman97
Посмотреть сообщение
The way I did this, like if you had to pick a random player id, was to store all of the possible choices in an array and then pick randomly from the array size. Maybe like this.

pawn Код:
new playerlist[MAX_PLAYERS],
    size;  // Maybe this is uneccessary and you could use sizeof

for(new i; i<MAX_PLAYERS; i++)
{
    if(IsPlayerConnected(i))
    {
        playerlist[size] = i;
        size++;
    }
}

new rand = playerlist[random(size) + 1];
Something like that I think.

Edit : Too slow...
Thanks for trying but doesn't really seem like what I want..
Reply
#6

Quote:
Originally Posted by Jack_Leslie
Посмотреть сообщение
/sarcasm Thankyou for telling me exactly how to do it.
I gave you an answer, if you wanted the script you could go to the request thread or the release section.
I assumed you wanted to know how to do it.
Reply
#7

Quote:
Originally Posted by Jack_Leslie
Посмотреть сообщение
/sarcasm Thankyou for telling me exactly how to do it.
He actually did tell you how to exactly do it. If you can't comprehend simple scripting tactics, you shouldn't be guaranteeing a 100% from scratch RP gamemode (signature).

Also, looking at the number of topics you have created for coding support, you're not always going to get it. Also learn to post in the correct thread. There is one created specifically for asking others to create small scripts for you. This is scripting support, not scripting give-aways.
Reply
#8

Quote:
Originally Posted by Jack_Leslie
Посмотреть сообщение
Quote:
Originally Posted by cessil
Посмотреть сообщение
store the numbers in an array.
count the numbers in the array.
generate random number from the total numbers in the array.
select the random number from the array index.
/sarcasm Thankyou for telling me exactly how to do it.
I got that 100%. I would have told you the same thing.


1. store the numbers in an array.
new rand[] = {3, 7, 25, 66, 9};

2. count the numbers in the array.
new size = sizeof(rand);

3. generate random number from the total numbers in the array.
new randn = random(size);

4. select the random number from the array index.
new random_value = rand[randn];

How hard is that?
Reply
#9

Quote:
Originally Posted by Pghpunkid
Посмотреть сообщение
I got that 100%. I would have told you the same thing.


1. store the numbers in an array.
new rand[] = {3, 7, 25, 66, 9};

2. count the numbers in the array.
new size = sizeof(rand);

3. generate random number from the total numbers in the array.
new randn = random(size);

4. select the random number from the array index.
new random_value = rand[randn];

How hard is that?
I think it was pretty hard for him. Wonder if he started sweating.
Reply
#10

Gj cessil.. too many people are get spoon fed answers.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)