Choosing random number from 0 to 2 (random.nextInt(2); problem)

Discussion in 'Plugin Development' started by WATWtomanik3, Jul 30, 2015.

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

    WATWtomanik3

    Hello! I want to choose a random number from 0 to 2. I tried to do it and i came so far:

    Code:
                Player p = (Player) sender;
                Random random = new Random();
              
                int chance = random.nextInt(2);
    random.nextInt(2) doesn't work for me. What should i do ?
     
  2. Offline

    Eos

    @WATWtomanik3

    Code:
                            Random random = new Random();
    
                            int chance = random.nextInt(2);
                            if ( chance == 2 ) {
                                //proceed
                            }
     
  3. Offline

    WATWtomanik3

  4. Offline

    Eos

    [​IMG]
    Hmm, that's strange. You can see above i'm not getting any error, can I see a bit more of your code.
     
  5. Offline

    WATWtomanik3

    @Eos here you go :)

    Code:
    // COIN TOSSING \\
            if (cmd.getName().equalsIgnoreCase("coin")) {
                if (!(sender instanceof Player)) {
                Player p = (Player) sender;
              
                p.sendMessage(ChatColor.RED + "Random: " + "This command is for players only!");
                return true;
              
            } else {
              
                if (sender.hasPermission("random.coin")) {
                Player p = (Player) sender;
                Random random = new Random();
              
                int chance = random.nextInt(2);
                  
                if ( chance == 2 ) {
                    //proceed
              
              
                }
              
                } else {
                  
                    sender.sendMessage(ChatColor.RED + "Random: " + "You don't have permission to use that command!");
                    return true;
    }
            }
            }
     
  6. Offline

    Eos

    I saw no problem with your code so I decided to copy n' paste it myself and check if i'm getting the same exact error as you. I recommend you switch to Intellij

    [​IMG]
     
    Last edited: Jul 30, 2015
  7. Offline

    WATWtomanik3

    Currently im using ECLIPSE LUNA maybe i will switch in the future. Anyone else know how to fix this ?
     
  8. @Eos @WATWtomanik3
    Ugh... Random#nextInt(n) returns a number from 0 to n, with n not included, so Random#nextInt(2) returns either 0 or 1
     
  9. Offline

    WATWtomanik3

    Updated code a little bit I still have the same problem...

    CODE :

    Code:
            // COIN TOSSING \\
            if (cmd.getName().equalsIgnoreCase("coin")) {
                if (!(sender instanceof Player)) {
                Player p = (Player) sender;
               
                p.sendMessage(ChatColor.RED + "Random: " + "This command is for players only!");
                return true;
               
            } else {
               
                if (sender.hasPermission("random.coin")) {
                      Random object = new Random();
                      for (int counter = 1; counter <= 1; counter++) {
                         
                        int number = 1 + object.nextInt(2);
                       
                        if (number == 1) {
                            // HEAD
                       
                        } else if (number == 2) {
                          // TAIL
                       
                        }
       
                    }
               
                } else {
                   
                    sender.sendMessage(ChatColor.RED + "Random: " + "You don't have permission to use that command!");
                    return true;
                   
                 }
              }
            }
     
  10. @WATWtomanik3
    1) You're first checking if the sender is a player, and if it's not, you cast from sender to Player >.<, hello ClassCastException
    2) That for loop is useless
    3) You're doing nothing if it's either heads or tails
     
  11. Offline

    Chiller

    @WATWtomanik3 Are you sure you imported the correct Random class. Check your imports at the top of the class and reply what the package is.
     
  12. You need to import java.util.Random
    And instead of the 0/1/2 number checking, for your purpose you can simply use:
    Code:
    if (random.nextBoolean()) {
    //code
    } else {
    //other code
    }
    And why don't you let the console run this command? I bet you are watching TheBCBroz... If you do: staaahp it
     
    ark9026 and Eos like this.
Thread Status:
Not open for further replies.

Share This Page