random - Randomness and probability in java -


i'm trying make small gambling game in java user has 2 options.

a textfield can write in amount of how wants bet , combobox can choose how many precent chance him win. combobox provides options 10%, 20%, 30%, 40% , 50%.

my problem don't know how apply probability in java. tried doing following:

public class gamblinggame {  private int rdmnumber; private int wonvalue;  public int winorlose(int valuebetted, int precenttowin, label label, int attempts){       precenttowin = precenttowin/10;      rdmnumber= threadlocalrandom.current().nextint(precenttowin, 10 +1);       system.out.println("this rdmnumber: " + rdmnumber);     system.out.println("this precentowin number: " + precenttowin);      //if number rolled equal precent choose (ie if 10, number 1, if 20 number 2)     if(rdmnumber == precenttowin) {         wonvalue = valuebetted/precenttowin *2;          system.out.println("you win!");          label.setstyle("-fx-text-fill: green");         label.settext("you won! recieved: " + wonvalue+" coins!");     }      else{         label.setstyle("-fx-text-fill: red");         label.settext("gamble failed! lost: " +valuebetted + " coins. ("+attempts+")");         wonvalue = -valuebetted;      }       system.out.println(wonvalue);     return wonvalue;    } 

}

the problem if, instance, user puts in 50% in combobox won't true 50% chance win.

the number dice need roll here 5 in order win, , max value 10. user roll number between 5-10 , when hits 5, he'd win.

how make precentage of rolling dice true 50%? (and of course not ruin if put in difference precentage parameter wont ruin method)

how try function?

private static random rd = new random();     public static boolean winner(int prob)     {         int value = rd.nextint(100);         system.out.println("got: "+value+" probability of winning:"+prob+"%");         if(prob>value)             return true;         else             return false;     }    

in case, probability num should bigger obtained random number. so, if run this.. work

  public static void main(string[] args) {                 //sure winner                 system.out.println(winner(99));                 //random winner                 system.out.println(winner(50));                 //not chance                 system.out.println(winner(10));             } 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -