Random Number 1/40 Chance

Discussion in 'Plugin Development' started by AstramG, Mar 10, 2013.

  1. Offline

    AstramG

    How could I make something occur on a 1 out of 40 chance, I'm using this for dropping mob heads at a 2.5% chance and I can't figure out how to make this happen at this 1/40 chance.
     
  2. AstramG
    Create a random number generator that goes to 39 (because it starts from 0)
    Then say:

    Code:java
    1. if ("that random number" == 1){
    2. //do something
    3. }
     
  3. Offline

    GodzOfMadness

    you could do a switch statement so it's not like if else if else if else if else if
     
  4. Code (Text):
    1.  
    2. Random r = new Random();
    3. int rand = r.nextInt(41);
    4. if (rand == 33) {
    5.  Do stuff...
    6. }
    7.  
    Rand will be a number between 0-40 (0 and 40 included). You can take any number, I just did 33
     
  5. Offline

    AstramG

    Well, I understand the approach but I'm a bit confused on creating a random number generator, assistance in creating one would be nice :)
     
  6. Offline

    AstramG

    Ah, thank you, but just for understanding why is nextInt 41? Why not 40?
     

  7. He explained it already :)
     
  8. AstramG look at my post :D
    EDIT: nextInt will generate a number between 0 and arg-1. So nextInt(2) will give a number between 0 and 1 with 0 and 1 included
     
  9. No if you want a 1/40 chance, just use 39 there, because a number generator goes from 0 to <a choosen number>, 0 included, so use 39
     
  10. TomTheDeveloper molenzwiebel
    You're all wrong.
    You use randomGenerator.nextInt(40). That generates 40 numbers. It starts at 0, it ends at 39.
    So you don't use .nextInt(39), because it only gives 39 numbers, and you don't use .nextInt(41) either, because it gives 41 numbers.
     
    hapm likes this.
  11. Clean and simple way:
    Code (Text):
    1. int n = 1 + (int)(Math.random() * 40);
    2.             if(n == 1){
    3. //Do Stuff >:D
    4. }
     
  12. Rprrr
    Oh, sorry then, I was wrong, sorry

    Thx to correct me
     

Share This Page