Probability of the Java method



  • There's a cycle of four methods. I want to understand how to make methods not every step of the cycle, but with the probability:

    • First: 60%
    • Second: 10%
    • third: 15 per cent
    • Fourth: 15 per cent

    Is there a special method? I can't understand the logic.



  • Simple solution based on standard casual cell generator:

    Random rnd = new Random();
    

    int b = rnd .nextInt(100);

    if (b < 60)
    method1(); // 60%
    else if (b < 70)
    method2(); // 10%
    else if (b < 85)
    method3(); // 15%
    else
    method4(); // 15%


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2