Jump to content
  • 0

[Help]Random Player from 1 fastlist


Question

Posted

Hello all i need help at something, cause i have not tried it again. Anyway...

Let's take an example:

I want from a fastlist (type L2PCINSTANCE) to select 1 player , and he becomes hero.The player will be selected randomly, how can i do it?

i have tried:

int r;
   FastList <L2PcInstance> players = new FastList<L2PcInstance>();
   {
   
   for(L2PcInstance p: players){
   r = Rnd.get(p.getObjectId());
   }
   
   for(L2PcInstance p2 :players){
   if(r == p2.getObjectId())
	   p2.setHero(true);
   }

 

but i dont think it will work, can anyone help me with this? :D

5 answers to this question

Recommended Posts

  • 0
Posted

Generate a random number between 0 and the size of the player list minus one. Then do a players.get(the_random_number)

:

int r;
   FastList <L2PcInstance> players = new FastList<L2PcInstance>();
   {
   
   Random dice = new Random();
   int counter = 0;
   while(counter < 1){
   r = dice.nextInt(players.size());
   counter++;
   }
   
   L2PcInstance p = players.get(r);
   
  }

 

?

  • 0
Posted

availableMembers.get(Rnd.get(availableMembers.size()));

c/p from L2Party.java

yea thanks.....i just didn't think the way.

 

also this works(for sure above one is better):

int r;

  FastList <L2PcInstance> players = new FastList<L2PcInstance>();

  {

 

  Random dice = new Random();

  int counter = 0;

  while(counter < 1){

  r = dice.nextInt(players.size());

  counter++;

  }

 

  L2PcInstance p = players.get®;

 

  }

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...