Solved Random, not being random...

Discussion in 'Plugin Development' started by Whats for Today, Nov 10, 2012.

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

    Whats for Today

    Code:java
    1. if(commandLabel.equalsIgnoreCase("half")){
    2. int onp = getServer().getOnlinePlayers().length;
    3. if(onp >=2){
    4. int rp = new Random().nextInt(getServer().getOnlinePlayers().length/2);
    5. Player halfone= getServer().getOnlinePlayers()[rp];
    6. Player halftwo= getServer().getOnlinePlayers()[++rp];
    7. halfone.sendMessage("Half One");
    8. halftwo.sendMessage("Half Two");
    9. }else{
    10. Player player = (Player) sender;
    11. player.sendMessage(ChatColor.RED + "Not enough people!");
    12. }
    13. }


    It seems to be picky -.-. It chooses the first player to join the server every time to be in half one, the second player to join is always in half two.
     
  2. what are you trying to do exactly, as the methode your using, always picks up the last player as player two
     
  3. Offline

    thehutch

    Are you trying to split everyone online into two groups :confused:
     
  4. Offline

    Whats for Today

    Yup pretty much :) Well randomly split them into two groups :p
     
  5. Offline

    thehutch

    Here is something I came up with to split the players online up into 2 groups :D

    Code:java
    1.  
    2. private Player[] shuffleArray(Player[] players) {
    3. Random rand = new Random();
    4. Player temp;
    5. int x;
    6. for(int i=0 ; i<players.length ; i++) {
    7. x = rand.nextInt(players.length);
    8. temp = players[i];
    9. players[i] = players[x];
    10. players[x] = temp;
    11. }
    12. return players;
    13. }
    14.  
    15. if(commandLabel.equalsIgnoreCase("half")){
    16. Players[] onp = shuffleArray(getServer().getOnlinePlayers());
    17. if(onp.length > 1) {
    18. for(int i=0 ; i<onp.length ; i+=2) {
    19. onp.sendMessage("Group 1"); // You can change what you
    20. onp[i+1].sendMessage("Group 2"); // do to these players
    21. } else {
    22. Player player = (Player) sender; // I assume you checked that the sender was a Player
    23. player.sendMessage(ChatColor.RED + "Not enough people!");
    24. }
    25. }
    26. [/i][/i]
     
    Whats for Today likes this.
  6. Offline

    raGan.

    Wait a second, you assign (Player[]) players to (Player) temp ? and then (Player) players[x] to (Player[]) players ? what sorcery is this
     
  7. Offline

    thehutch

    You use it in a BubbleSort algorithm so I guess it'll work, all it does is switch 2 values in an array around

    EDIT: fixed it :p, seems I forgot to add the [ i ]:D
     
    Whats for Today likes this.
  8. Offline

    raGan.

    Looks like sorcery no longer.
     
  9. Offline

    Karl Marx

    Another tip when working with random: create a Random object in onEnable(), and reuse it. More specifically, don't create a new Random every time the method is called. If you don't specify a seed value to the Random constructor, then it will use the current time. If your code is called multiple times within the same second, then every call will get the same "random" sequence of values, producing somewhat less than random results :p
     
  10. actually, the random methode is based of nanoseconds when its created, not seconds
     
  11. Offline

    Whats for Today

    shuffleArray, I feel like that is something I should have learned.
    I have a couple of questions... because I'm still a little confused. On line 16 I believed that is supposed to be Player instead of Players, correct? Line 26, don't even know what to ask about this. I thought I needed a public boolean above line 15, am I mistaken? Last thing is that it didn't like onp by itself, so I put onp[1], is that alright?
     
  12. Offline

    thehutch

    It is meant to be Player[] on line 16, that was a spelling mistake because I wrote it in Sublime Text 2 :D

    Lines 15-26 was the code you gave me, I guess it's meant to be inside your onCommand() method wherever it was before. Just simply add the other method shuffleArray() outside of onCommand().
     
    Whats for Today likes this.
  13. Offline

    Whats for Today

    Ok. I had the shuffleArray outside the on command, awesome thats done. The "[/i][/i]" is outside of the onCommand... Now I just have a dumb little syntax error telling me to close class body and the method body even though they already are.
     
  14. Offline

    thehutch

    Post the code for the entire class, I think you have a missing bracket somewhere, also the [/i] is from Bukkit's stupid formatting of text and wants to italicize everything :D
     
    Whats for Today likes this.
  15. Offline

    Whats for Today

    I was wondering about the "[/i][/i]" it was making absolutely no sense to me xD. Anyways I fixed the problem :D Everything looks fine with the code now, just have to get my buddy on to test :p Thanks for the support.
     
  16. Offline

    Karl Marx

    I suppose you've got a point there. I actually meant milliseconds (my mistake,) but I see that recent versions of the JVM do, in fact, use nanoseconds if available.

    Either way, I've made the mistake before, and I recommend avoiding it as a general "best practice"- no matter how unlikely it is to occur. Tracking down intermittent, "random" bugs in your code can be a very frustrating experience- especially in larger projects- so it's a good habit to get into ;)
     
Thread Status:
Not open for further replies.

Share This Page