10 out of 100 % chances??

Discussion in 'Plugin Development' started by BeastCraft3, Apr 17, 2015.

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

    BeastCraft3

    Hellau, I'm BeastCraft3. Am wondering a qustion for quite some time now.
    I recently made a plugin with 10 commands. When someone walk on sponge one of the commands will be executed. but how do I make it so it's a 10% chance that you it either do /command 1, /command 2, /command 3 .....etc
    I think you understand it. And if your wondering which commands I made it is a warp plugin.

    HOpe you understand what I need, if not feel free to ask. k bye!
     
  2. Offline

    nverdier

  3. Code:
    if (new Random().nextInt(100) <= 10) {
     
  4. Offline

    BeastCraft3

    @FisheyLP
    like
    Code:
    EventHandler
    Check if walking on sponge
    if (new Random().nextInt(100) <= 10) {
    Commands???
    }
     
  5. Offline

    nverdier

    @FisheyLP Or just get a random number 0-9 and get that entry from an ArrayList.
     
  6. Offline

    BeastCraft3

    @nverdier
    But will this work?
     
  7. Offline

    nverdier

    @BeastCraft3
    1) Store all of the commands in an ArrayList.
    2) When the user walks on sponge.
    3) Get a random integer from 0-9, and get the command at the random index from the ArrayList.
    4) Run the command.
     
  8. Or just arraylist.get(new Random().nextInt(arraylist.size());

    Which gets a random object of the arraylist
     
  9. Offline

    nverdier

    @FisheyLP Which is what I said. Except I was using 0-9 because that's what the OP said.
     
  10. Offline

    BeastCraft3

    @nverdier @FisheyLP
    ok, I havent coded for like 6 months, any more help in code in general? like a small template I could get using the
    //todo ??
     
  11. new Random().nextInt(x) returns a number between 0 and x-1.
    If x is (for example) 10, it generates a random number between 0 and 9.
    And if you want to check the % use this:
    if (new Random().nextInt(100) <= x) {
    Where x is the chance in %, that the if-condition will be true.
    If you want to get a random object of a ArrayList, you can use this:
    .get(index) //starts with 0
    .size() //returns the amount of objects in the ArrayList
    You just need to get the object with the index (random from 0 to ArrayList size)
    Done :D
     
  12. Offline

    BeastCraft3

    @FisheyLP @FisheyLP
    Sorry for being a little bad here, but can you help me with the Arraylist stuff, like how do I store the commands in it?
     
  13. Offline

    BeastCraft3

  14. CodePlaysMinecraft likes this.
  15. Offline

    stormneo7

    These are all the constructors you will need.
    Code:
    Player.chat("/examplecommand 1");
    // Or you could use dispatch command but whatever.
    
    new Random().nextInt(10);
    
    @EventHandler
    public void onMove(PlayerMoveEvent evt)
    I'm just inferring you know the basics of Java and the basics of an event handler...
     
  16. Offline

    BeastCraft3

    @stormneo7
    I know how to do the that ;/ I'm just asking how I store commands in an arraylist?!
     
  17. Offline

    nverdier

    CodePlaysMinecraft likes this.
  18. Offline

    stormneo7

    Commands are stored using the Command variable but could be stored using Strings.
    http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

    Variables used to create an object or instructions. What kind of question is this...?
     
  19. Offline

    nverdier

    No.
     
  20. Little tutorial:
    Show Spoiler
    1. Create a new array:
    Code:java
    1. String[] commands = {"/command1", "/command2", "/command3"};

    2. Get a random Object (String) of the array:
    Code:java
    1. String random = commands[new Random().nextInt(commands.length)];

    3. Learn java because you don't even know what an array is / how to define one.
     
  21. Offline

    nverdier

    @FisheyLP Subtract one from the length though.
     
  22. @nverdier No.
    And array[x] starts with the object at 0th place.
    Example:
    Code:java
    1. String[] array = {"String 1", "String 2", "String 3"};
    2. System.out.println(array.length); //Output: 3
    3. System.out.println(array[0]); //Output: String 1
    4. System.out.println(array[2]); //Output: String 3
     
  23. Offline

    1Rogue

    Constructing a new random every time will just end up with the same results. Use ThreadLocalRandom or Math#random
     
  24. Offline

    nverdier

  25. Offline

    stormneo7

    No? What is it then?
     
  26. @1Rogue
    No. .nextInt(x) always returns a new generated random number.
    [​IMG]
     
  27. Offline

    nverdier

    @stormneo7 Look it up. You're somewhat close, but not there.
     
  28. Offline

    1Rogue

    Whoops, thought you were setting the seed too.

    It's still a terrible idea, you don't need to instantiate a new Random for every single random number when you can just use a single ThreadLocal instance
     
  29. Generating a new Random every time, is 5 times slower than having a single Random defined. (tested it :p)
     
Thread Status:
Not open for further replies.

Share This Page