Chose a random player

07/25/2010 10:29 dodolinobobo#1
Now i'm coding a event...but i need to do somethink for to get a Random Player, can i do this random think with a static List? or i must make a variable for every character?
07/25/2010 11:33 _DreadNought_#2
Simple!
do something like
Code:
foreach(character C in Game.World._H.Values)
{
//Hmmmmm.......
random code.
}
07/25/2010 12:00 dodolinobobo#3
i know to do the "foreach part",but i don't know how to chose a random one from the list..
07/25/2010 12:04 .Kob#4
Quote:
Originally Posted by Eliminationn View Post
Simple!
do something like
Code:
foreach(character C in Game.World._H.Values)
{
//Hmmmmm.......
random code.
}
No comments...

You can do a list and make a random with that list.


List<string> Names = new List<string>();

Every time something join, Names.add(player);

And later,
Quote:
Random N = new Random();
int NumberSelected = N.Next(Names.Count);
But this gives you a number. You need the player.

You need: using System.Linq; for the next action.

string PlayerSelected = Names.ElementAt(NumberSelected);

Done.
07/25/2010 12:49 dodolinobobo#5
thanks,i think i got it
07/25/2010 15:27 _DreadNought_#6
Well for events, in my case I have a dictionary for them. When the player signs up they get added to it and if I need to do a random its very easy.