2

Im looking for a method where I can randomly generate stats for a character (like Skill, Attack, Defense, ...). Lets say my stats go from

1 - 100

Now I want that stats between

1 - 30 a probability of 30%

31 - 50 a probability of 45%

51 - 75 a probability of 20%

76 - 100 a probability of 5%

I know I can probaly use Random class or Math.random() but not sure how.

Thanks in advance!

Carlos
  • 5,991
  • 6
  • 43
  • 82
JoCoaker
  • 633
  • 9
  • 18
  • Try this one http://stackoverflow.com/questions/20327958/random-number-with-probabilities/20328491#20328491 – Piyush Gupta Feb 11 '16 at 09:18
  • @JoCoaker Do you intend to generate `31-50` with a probability of 45% of the time? Or do you want to generate these values 20% of the time? – Tim Biegeleisen Feb 11 '16 at 09:34
  • @TimBiegeleisen Well I would want it to have a probality of 45%, but inside of your constructon below, i can set new values for the random generater etc(31 - 50) and then generate the actual value :) So your answer Help me – JoCoaker Feb 11 '16 at 09:36

2 Answers2

1

One option is to generate a random number in the range of 0 to 100 and then use a series of if-else statements to determine which stats to generate for your character:

public void printRandomStats() {
    Random random = new Random();
    int next = random.nextInt(101);

    if (next <= 30) {
        // this will happen 30% of the time
        System.out.println("Generating stats from 1-30");
    } else if (next <= 75) {
        // this will happen 45% of the time
        System.out.println("Generating stats from 31-75");
    } else if (next <= 95) {
        // this will happen 20% of the time
        System.out.println("Generating stats from 76-95");
    } else {
        // this will happen 5% of the time
        System.out.println("Generating stats from 96-100");
    }

    return;
}
JoCoaker
  • 633
  • 9
  • 18
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

Best solution to your problem is to create a nonuniform probabilistic value list. Then randomly pick a value from that list. For example:

If we have a list like below:

{5 , 5 , 5 , 5 , 10 , 10 , 10 , 20, 20 ,30} 

Our probabilities will be like that;

5 => 40%  ---  10 => 30%  ---  20 => 20%  ---  30 => 10%

You can achieve that solution with the simple method below:

private static int generateStat()
{

    ArrayList<Integer> stats = new ArrayList<Integer>();

    //first parameter is probability and second is the value
    stats.addAll(Collections.nCopies(30, (int)(Math.random()*30))); 
    stats.addAll(Collections.nCopies(45, (int)(Math.random()*20)+30));
    stats.addAll(Collections.nCopies(20, (int)(Math.random()*25)+50));
    stats.addAll(Collections.nCopies(5,  (int)(Math.random()*25)+75));

    return stats.get((int)(Math.random()*100));
}
Raptor
  • 187
  • 1
  • 2
  • 11
  • I have one question: Is it posiable to also have decimal probablity? like 4.5% and 0.5% ? – JoCoaker Feb 11 '16 at 10:48
  • Found out how! (Add '0' at the end of the probability and multiplay by 1000) – JoCoaker Feb 11 '16 at 10:54
  • @JoCoaker Yes, of course it is possible. But it is not like you said. If you add '0' at the end of the probabilities and multiply random index by 1000, you will get exactly the same probability. You need to customize the probabilities by changing the list size and contents as you want. Or second way; if you dont need to fullfill 100% percent of probabilty like "item drop rates in games (item box may be empty)" , you can just multiply the random index by 1000 instead of 100 and handle "IndexOutOfBoundException"(empty result) as you want. – Raptor Feb 11 '16 at 11:44
  • Hmm. I get the second way, but im not quite should i get what you mean with the first way. Would you be so kind and add an example of that to your answer? :) – JoCoaker Feb 11 '16 at 12:00
  • @JoCoaker For example add this fifth line under add operations: stats.addAll(Collections.nCopies(900, -1)); and change return statement like that: return stats.get((int)(Math.random()*1000)); Now if you get '-1' as result, that means "empty". And your real probabilities will decrease 10 times. – Raptor Feb 11 '16 at 12:19
  • Okay everything perfect! Played around, works like a carm! Thanks! :) – JoCoaker Feb 11 '16 at 12:50