2 different random online players

Discussion in 'Plugin Development' started by Wingas, Sep 8, 2014.

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

    Wingas

    Hello, I need to make 2 random players will get message, but player can't be same, they must be 2

    I tried to do, but my code is failed

    Code:java
    1. Player player1 = Bukkit.getOnlinePlayers()[random];
    2. Player player2 = Bukkit.getOnlinePlayers()[random];
     
  2. Offline

    jpjunho

    Wingas
    Random r = new Random();
    p1 = onlineplayers[r.nextInt];
    p2 = onlineplayers[r.nextInt];

    Note that there's a chance that they are the same
     
  3. Offline

    Necrodoom

    random cannot hold 2 different values at the same time.
    Call random method again for a new value.
     
  4. Offline

    Wingas

    Bump
     
  5. Offline

    Rocoty

     
  6. Offline

    xize

    what you could do:

    1. you create a final Random field in your class.

    2. then start the BukkitRunnable when the online length is more than 2.

    3. and then per tick you count in int i = rand.nextInt(playercount); //this changes everytime when the scheduler ticks because the Random is instanced outside the methods as a field ;-)

    4. then do your checks.
     
  7. Wingas If you take the value from getOnlinePlayers() then it's possible, however unlikely, for the same person to be picked twice. Also, getOnlinePlayers() returns a collection now, not an array. Take a copy of the collection, take one player from it, remove the player from that collection, and then take another player.
     
  8. Offline

    Wingas

    AdamQpzm xize Rocoty jpjunho
    Code:java
    1. int random = new Random().nextInt(Bukkit.getOnlinePlayers().length);
    2. Player player = Bukkit.getOnlinePlayers()[random];
    3. Player surv = Bukkit.getOnlinePlayers()[random];
    4. murd.add(player);
    5. if(murd.contains(surv)){
    6.  
    7. }
    8. for (Player p : on())
    9. if(murd.contains(p)){
    10. p.sendMessage("Ur murd");
    11. }


    How to make if(murd.contains(surv)), it tries to take another one?
     
  9. Wingas See, that's the problem with this snippet that's already been pointed out.

    Assuming getOnlinePlayers returns {Wingas, AdamQpzm, Rocoty}

    PHP:
    int random = new Random().nextInt(Bukkit.getOnlinePlayers().length); // random is now 1
    Player player Bukkit.getOnlinePlayers()[1]; // 1 is the value of random, AdamQpzm chosen
    Player surv Bukkit.getOnlinePlayers()[1]; // 1 is still the value of random, AdamQpzm chosen
    Please learn the basics of Java before trying plugin development :)
     
  10. Offline

    Wingas

    AdamQpzm you still dont give help to me :/
     
  11. Wingas Well I don't know what you expect me to do. If you don't know enough Java, I cannot help you make plugins.
     
  12. Offline

    TGRHavoc

    Wingas
    What AdamQpzm is saying in simpleton terms:
    In class a teacher assigns everyone a number. The teacher then picks a "random" number and writes it down. The teacher then picks the pupil with that number.
    See the problem? The teacher is never picking another number and is therefore only picking one student. The same is happening with your plugin. You're setting the number to one value then assuming it's going to magically change (which it wont..) and you're therefore only picking one player.
     
  13. Offline

    Unica

    ...
    Which means you have to make 2 random functions
     
Thread Status:
Not open for further replies.

Share This Page