Choice a random Player?

Discussion in 'Plugin Development' started by Uniclaw, Aug 31, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Uniclaw

    Hi!

    i have tryed to choice a random player from all online players - but failure.
    I have get the number of online players, and then make a random - but how with this random i can choice a player?

    Code:
    int laenge = Bukkit.getServer().getOnlinePlayers().length;
    int random = (int)(Math.random() * laenge);
     
  2. Offline

    p000ison

    Math.random returns a random value from 0 to 1.

    Player random = Bukkit.getOnlinePlayers()[new Random().nextInt(Bukkit.getOnlinePlayers().lenght)]
     
  3. Offline

    Uniclaw

    Thanks!! But how you convert from [new Random().nextInt(Bukkit.getOnlinePlayers().lenght)] to a player ?
     
  4. Offline

    makskay

    The line of code posted above returns a Player object because it's using the random integer generated by "new Random()..." as an index within the array of Players returned by Bukkit.getOnlinePlayers(). It's a more compact version of the following:
    Code:
    int random = new Random().nextInt(Bukkit.getOnlinePlayers().length);
    Player player = Bukkit.getOnlinePlayers()[random];
    
     
    lukewizzy likes this.
  5. Offline

    Uniclaw

    Ah, ok, thanks!
     
Thread Status:
Not open for further replies.

Share This Page