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®;

 

  }

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×
×
  • Create New...

Important Information

This community uses essential cookies to function properly. Non-essential cookies and third-party services are used only with your consent. Read our Privacy Policy and We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue..