-2

I would like to create some numbers each time based on a probability.

Example:

Numbers from : 1-5

Number 1 has probability to be created by 0.55,

Number 2 has probability to be craeted by 0.25,

Number 3 has probability to be craeted by 0.10,

Number 4 has probability to be craeted by 0.05,

Number 5 has probability to be craeted by 0.05

In this case, it has to usually create number 1, and often number 2, sometimes number 3 and rarely number 4 or 5

So, the "method": create_the_numbers(int ending_number) should be like this pseado-code:

public int create_the_numbers(int ending_number){
return probability(Random.getInt(ending_number))
}

There is an question for the C# here: Probability Random Number Generator

Community
  • 1
  • 1
  • Could you rephrase the first sentence in your question? It's quite hard to understand what you are looking for. – sprinter Mar 29 '16 at 01:26
  • Total probability is `1.4`. It should be `1.0` (or `100%`), otherwise you're talking about weighing factors, not probabilities. – Andreas Mar 29 '16 at 01:27
  • I think that my example is clear. but I will edit my Q – user6126842 Mar 29 '16 at 01:28
  • @Andreas i fixed the probablity issue in my exmaple. u have right – user6126842 Mar 29 '16 at 01:29
  • The first sentence is not grammatical. – sprinter Mar 29 '16 at 01:29
  • 2
    This looks like homework, and whether it's homework or "home work", you should show us your best good faith attempt to solve it. This serves several purposes including showing us directly what misconceptions or errors you may have, and also that you're willing to put in your own thoughts and efforts in solving this and aren't simply asking for an academic handout. – Hovercraft Full Of Eels Mar 29 '16 at 01:32
  • it's not actually a homework, neither a "home work". It is going to implemented in a pseado-random creation of some objects. However, @HovercraftFullOfEels the point here is not the reason of asking the question, but if there is any solution on this. – user6126842 Mar 29 '16 at 01:35
  • Also, for everyone who down-voted it, down vote and this one: http://stackoverflow.com/questions/3016439/probability-random-number-generator?rq=1 which is the exact same question but it is in C#. Smarts. – user6126842 Mar 29 '16 at 01:37
  • You still could have posted an attempt. Shame on you for not doing so. – Hovercraft Full Of Eels Mar 29 '16 at 01:42
  • @HovercraftFullOfEels i posted a (probably wrong) "pseudo-code" and how I could imagine it. I have to up-vote you for your "help" and your ideas. **You forgot the stackoverflow existence reason**.. **Don't criticise and don't flaming about homeworks** because you have good reputation. Just for the records, I don't see your "answer" down there btw. – user6126842 Mar 29 '16 at 01:48
  • Possible duplicate of [How to pick an item by its probability?](http://stackoverflow.com/questions/9330394/how-to-pick-an-item-by-its-probability) – Austin Mar 29 '16 at 02:24
  • The solution to http://stackoverflow.com/q/9330394/3795219 applies here: Just use whole numbers instead of fractions and you're done. As a side note, this process is commonly called *fitness proportional selection* or *roulette wheel selection* in the evolutionary algorithm literature. – Austin Mar 29 '16 at 02:31

2 Answers2

0

I believe you are asking how to weight probabilities. A simple method is as follows:

int getWeightedRandomInt(double... weights) {
    final Random random = new Random();
    double val = random.nextDouble();
    for (int i = 0; i < weights.length; i++) {
        if (val < weights[i])
            return i;
    }
    throw new IllegalArgumentException("badly formed weights");
}

This is used by passing sorted cumulative weights. In your example:

getWeightedRandomInt(0.55, 0.8, 0.9, 0.95, 1.0);

If you are using Java 8:

Arrays.stream(weights).filter(w -> val < w)
    .findFirst().orElseThrow(IllegalArgumentException::new);
sprinter
  • 27,148
  • 6
  • 47
  • 78
0

The following method will do it:

private static int get() {
    double d = Math.random();
    if (d < 0.55) return 1; // 0.55
    if (d < 0.80) return 2; // 0.25
    if (d < 0.90) return 3; // 0.10
    if (d < 0.95) return 4; // 0.05
    return 5;               // 0.05
}

Using Math.random() is simple, but has it limitations. Read the javadoc. Alternatively, use Random.

Andreas
  • 154,647
  • 11
  • 152
  • 247