Random Selection

10/23/2011 23:38 abdeen#1
Solved Thanks
10/24/2011 00:11 pro4never#2
Random selection to select players...

Take a collection (list, dictionary, hashtable doesn't matter) of all eligible players and then just do a random from 0-collection length and then select (and remove) member from that index in the collection.


Example...

List<Player> players = new List<Player>();
foreach(Player p in Kernel.Clients.Values)
players.Add(p);

//You now have a list of all players. Most cases youd be checking maps and stuff but w/e!

Random rand = new Random();

//You want to select two players... I'm not sure how you want to do this either a like StartEvent(PlayerOne, PlayerTwo) or w/e but for now I'll just pull two player objects and store them

Player pOne, pTwo;

int selectedIndex = rand.Next(players.Length);
pOne = players[selectedIndex;
players.RemoveAt(selectedIndex);//this way we can never select same char twice
selectedIndex = rand.Next(players.Length);
pTwo = players[selectedIndex];
players.RemoveAt(selectedIndex);


And now you can do w/e you like with the two pulled player objects
10/24/2011 00:32 abdeen#3
Solved Thanks
10/24/2011 01:40 pro4never#4
It was an example...

Kernel.Clients was an example of where your source keeps track of all online players.

I forgot list uses count instead of length. Same thing though. It was example code, not something you can expect to just paste into your source and have it work.
10/24/2011 02:04 abdeen#5
Quote:
Originally Posted by pro4never View Post
It was an example...

Kernel.Clients was an example of where your source keeps track of all online players.

I forgot list uses count instead of length. Same thing though. It was example code, not something you can expect to just paste into your source and have it work.
so what i have to use instead of Player ? or do i need to create new class for it ?
i used GamePool instead of Client and worked
i used Entity instead of player " Player Not Players " and now i want to know what the mean of W/e and i wrote after

players.RemoveAt(selectedIndex);

client.Entity.Teleport(ID, X, Y);
but says : the name " client " dose not exist in the current context .

so what or how i can modify it to send the selected players to the map which i want ?


10/24/2011 06:09 thesamuraivega#6
Hi,
My Random Selection my way to select 1 players from online players and send to terrorist state.

Code:
public static void RandomSelect()
        {
            List<Client.GameState> players = new List<Client.GameState>();
            foreach (Client.GameState p in ServerBase.Kernel.Clients.Values)
                players.Add(p);
            Random rand = new Random();
            Client.GameState pOne;
            int selectedIndex = rand.Next(players.[COLOR="Red"]Length[/COLOR]);
            pOne = players[selectedIndex];
            players.RemoveAt(selectedIndex);//this way we can never select same char twice
                pOne.Entity.AddFlag(Network.GamePackets.Update.Flags.Flashy);
                pOne.Entity.KillTheTerrorist_IsTerrorist = true;
                Conquer_Online_Server.Network.PacketHandler.WorldMessage("Ahora " + pOne.Entity.Name + " Es el terrorista!!!");
        }
int selectedIndex = rand.Next(players.Length);´

why?
10/24/2011 07:11 BaussHacker#7
Quote:
Originally Posted by thesamuraivega View Post
Hi,
My Random Selection my way to select 1 players from online players and send to terrorist state.

Code:
public static void RandomSelect()
        {
            List<Client.GameState> players = new List<Client.GameState>();
            foreach (Client.GameState p in ServerBase.Kernel.Clients.Values)
                players.Add(p);
            Random rand = new Random();
            Client.GameState pOne;
            int selectedIndex = rand.Next(players.[COLOR="Red"]Length[/COLOR]);
            pOne = players[selectedIndex];
            players.RemoveAt(selectedIndex);//this way we can never select same char twice
                pOne.Entity.AddFlag(Network.GamePackets.Update.Flags.Flashy);
                pOne.Entity.KillTheTerrorist_IsTerrorist = true;
                Conquer_Online_Server.Network.PacketHandler.WorldMessage("Ahora " + pOne.Entity.Name + " Es el terrorista!!!");
        }
int selectedIndex = rand.Next(players.Length);´

why?
players.Count.
10/24/2011 07:16 pro4never#8
Quote:
Originally Posted by thesamuraivega View Post
Hi,
My Random Selection my way to select 1 players from online players and send to terrorist state.

Code:
public static void RandomSelect()
        {
            List<Client.GameState> players = new List<Client.GameState>();
            foreach (Client.GameState p in ServerBase.Kernel.Clients.Values)
                players.Add(p);
            Random rand = new Random();
            Client.GameState pOne;
            int selectedIndex = rand.Next(players.[COLOR="Red"]Length[/COLOR]);
            pOne = players[selectedIndex];
            players.RemoveAt(selectedIndex);//this way we can never select same char twice
                pOne.Entity.AddFlag(Network.GamePackets.Update.Flags.Flashy);
                pOne.Entity.KillTheTerrorist_IsTerrorist = true;
                Conquer_Online_Server.Network.PacketHandler.WorldMessage("Ahora " + pOne.Entity.Name + " Es el terrorista!!!");
        }
int selectedIndex = rand.Next(players.Length);´

why?
That selects a random index for your selection.


Think of it like this... I have a deck of cards so I take a random number between 0 and the number of cards in the deck. I then take the Xth card in the deck.

That's what the code is doing exactly. I use an index based method and save the index simply because it makes pulling that user from the collection and then REMOVING him from the collection easier.

This is not needed if you're only taking 1 person though as you'd never be able to select the same person twice. This more has to do with taking multiple entries from small collections.

Lets say I have 4 players on a map and I need to at random pick two of them to fight.

There's a 1/4 chance for each person to be picked... but after I pick someone there's still a 1/4 chance of picking them AGAIN if I don't remove them from the possibilities.... which I do in the example I posted via RemoveAt


<edit>

oops sorry I was thinking you were asking the theory behind the code... not just why it was an 'error' (nothing in the code I posted is intended for use. It's an example rofl!)
10/24/2011 07:19 BaussHacker#9
Quote:
Originally Posted by pro4never View Post
<edit>

oops sorry I was thinking you were asking the theory behind the code... not just why it was an 'error' (nothing in the code I posted is intended for use. It's an example rofl!)
Copy-paste. That's how people code these days. :)
10/24/2011 17:18 abdeen#10
Solved Thanks
10/24/2011 17:24 Korvacs#11
Please dont claim to have written something when you clearly didnt, it throws off any information that people present to you because they believe you have knowledge of the subject when in actual fact you know nothing about it >.<.
10/24/2011 17:55 abdeen#12
Solved Thanks
10/24/2011 18:03 Korvacs#13
Then how is it that Pro4never posted an example and you just tried to copy paste it into your source and then couldnt fix simple issues with it >.<!

And are you serious with that execute void??
10/24/2011 18:07 abdeen#14
Solved Thanks