-4

I want to select 5 random numbers with some probability.

my numbers: 2-5-6-9-14

probability:
2  -> %30
5  -> %20
6  -> %35
9  -> %10
14 -> %5

I want to go new activity if the three numbers are the same.

Not: maximum three numbers can be the same.

How Can I do that?

Johny
  • 45
  • 10
  • (Just compute the probability of (at least/exactly?) three of five draws with the given probabilities yielding the same result and use a single draw.) `Not: maximum three numbers can be the same` - please explicate in the question what this means (Not / *Note* ?!). – greybeard Jul 25 '17 at 16:56
  • I can select the same number three times, not more. an example my random numbers should be 2, 2, 2, 6, 14 not 2, 2, 2, 2, 14. Sorry for my bad English. @greybeard – Johny Jul 25 '17 at 17:02
  • If you just want to decide whether or not to `go new activity` with specified probability/-ies, what use is actually handling individual probabilities (30, 20, 35, 10, 5)/numbers (2, 5, 6, 9, 14)? – greybeard Jul 25 '17 at 17:08

3 Answers3

1

the easy way : make an array with 100 element and put inside it 30 item of 2, 20 item of 5 and so on, then run your random on the array, not the best solution but the easiest one.

Oussema Aroua
  • 5,225
  • 1
  • 24
  • 44
0

the easiest way would be to add the required nubers more than once into an array and just select a random index.

Also you might want to have a look at this Random number with Probabilities

BooNonMooN
  • 250
  • 3
  • 13
0

You should try something like that (can be massively improved). The idea is to generated a number between 0 and 100 (representing 100%). And then you return the number you want based on the probabilities:

public int random() {
    Random r = new Random();
    int n = r.nextInt(100);

    if (n < 30) // 30%
        return 2;
    if (n < (30 + 20)) // 20% (we exclude the 30% above)
        return 5;

    if (n < (30 + 20 + 35))  // 35% (we exclude the ones above)
        return 6;

    if (n < (30 + 20 + 35 + 10)) // 10% (30 + 20 + 35 are the previous probabilities)
        return 9;

    return 14; 
}
Eselfar
  • 3,759
  • 3
  • 23
  • 43
  • I accepted your answer. I want to do something if selected three numbers are the same. How can I do that? – Johny Jul 25 '17 at 16:46
  • We can't code your entire app for you you know... But, what you can do is to store every selected elements in an array. Then when the fifth element is selected, loop thought the array and calculate how many time each element appears. If one appears 3 times you do something. – Eselfar Jul 25 '17 at 17:21