Picking a random number from a list -
Jack_Leslie - 13.07.2011
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?
Re: Picking a random number from a list -
cessil - 13.07.2011
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.
Re: Picking a random number from a list -
Jack_Leslie - 13.07.2011
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.
Re: Picking a random number from a list -
Backwardsman97 - 13.07.2011
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...
Re: Picking a random number from a list -
Jack_Leslie - 13.07.2011
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..
Re: Picking a random number from a list -
cessil - 13.07.2011
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.
Re: Picking a random number from a list -
Bakr - 13.07.2011
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.
Re: Picking a random number from a list -
Pghpunkid - 13.07.2011
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?
Re: Picking a random number from a list -
Steven82 - 13.07.2011
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.
Re: Picking a random number from a list -
PrawkC - 13.07.2011
Gj cessil.. too many people are get spoon fed answers.