[Basic Tutorial] Random chances of happening

Discussion in 'Resources' started by Jaker232, Nov 28, 2011.

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

    Jaker232

    Code:java
    1.  
    2. Random random = new Random();
    3. int Chance = random.nextInt(200);
    4.  


    This will import a random utility that you can generate numbers from 0 to 200. You can change 200 to any number you want out of it.

    Code:java
    1.  
    2. if(Chance >= 100) {
    3. }
    4.  


    The >= is in mathematical statement, "greater than or equal to". So if our generated number was 137, the plugin will read it like..
    Code:
    137 > 100
    
    which passes it onto the next block of code. Let's see what we can do with this. Here's an example.

    Code:java
    1.  
    2. if(Chance >= 100) {
    3. player.sendMessage(ChatColor.GREEN+"You won the lottery!");
    4. }
    5.  


    If you want, you can make an else function.

    Code:java
    1.  
    2. if(Chance >= 100) {
    3. player.sendMessage(ChatColor.GREEN+"You won the lottery!");
    4. } else {
    5. player.sendMessage(ChatColor.RED+"No. You didn't win the lottery. D:");
    6. }
    7.  


    You can do a bunch of stuff with this. Be creative, my friends.

    All codes but examples are copied from CrazySword, copyrighted 2011.
     
    BaHeTo0, L3N and devilquak like this.
  2. Offline

    r3Fuze

    Nice tutorial, this will probally help some people.
     
  3. Offline

    Jaker232

    Thanks for the nice comment.
     
  4. Offline

    emericask8ur

    Nice man.
    Could also use
    Code:
    if ((int) (Math.random() * 4) == 0) {
    //Stuff
    }
    
     
  5. Offline

    iffa

    I face palmed a bit in rl. Pretty basic java here...
     
  6. Offline

    Sleaker

    Just so people don't get confused - the easiest way to do percentages is with < not >= (as it requires some extra math)
    Example using the random variable from above:
    Code:
    if (chance < random.nextInt(100)) {
        // Do Stuff!
    }
    
    The reason why you don't want to do >= is because as chance increases there should be a higher chance of success.

    Code:
    if (chance >= random.nextInt(100)) {
        // Do Stuff
    }
    
    The above code will actually have a less chance of success (inverse) - lets say you wanted a 90% chance of success, using the above code there will actually only be a 10% chance of success.

    One thing to not forget, random.next will always include 0 and exclude max. that means random.nextInt(100) will NEVER return 100, but will have a chance to return 0. So using the first code example is the proper (my opinion) way to check % chance directly via integers.
     
  7. Offline

    Jaker232

    Changed the title. You happy now?
     
  8. Offline

    lmc

    It may be basic to you and me, but their are other people out there that would find this useful. We all started from somewhere.
     
    WalkerCrouse likes this.
  9. Offline

    Pandemoneus

    Please try to follow the java code style conventions and properly name your variables.
    • All local variables should start with a lower case letter.
    • There is a whitespace after if.
    • There is a trailing and leading whitespace after the "+" operator.
    I know you will be upset because I am posting this, but should also help making other people's code readable.
     
  10. Offline

    Daniel Heppner

    this.
     
  11. Offline

    tips48

    +1
     
  12. Offline

    SkillEscalation

    I'm learning java right now and I did find that learning the syntax from this was helpful. Has a few errors as stated above, but overall it helped me. Thanks!
     
Thread Status:
Not open for further replies.

Share This Page