0

Based on the first answer in this question, I implemented a function that returns random numbers with a probability. However, the random numbers generated through this method are not following the probability described.

My code is given below

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

        int count1=(int) (type1prob*100);
        int total = 0;
        total+=count1;
        while(count1!=0){

            probsArray.add(1);
        count1--;
        }

        int count2=(int) (type2prob*100);

        total+=count2;
        while(count2!=0){

            probsArray.add(2);
        count2--;
        }

        int count3=(int) (type3prob*100);

        total+=count3;
        while(count3!=0){

            probsArray.add(3);
        count3--;
        }


        System.out.println("total is "+total);

        int randomNum= ThreadLocalRandom.current().nextInt(0, total);



    return probsArray.get(randomNum);

If I use the following input values

type1prob=0.5
type2prob=0.0
type3prob=0.5

Then the output I get is

Total type 1 = 25
Total type 2 = 0
Total type 3 = 20

There shouldnt be such a big difference between type 1 and type 3.

Community
  • 1
  • 1
statboy
  • 85
  • 8
  • 5
    IMHO 45 samples is way too few. Give it a few million samples, and see. – ppeterka Aug 25 '16 at 13:55
  • I only need less than 100 samples – statboy Aug 25 '16 at 13:56
  • 5
    But you can't test it with too few samples and then draw wrong conclusions. – Kayaman Aug 25 '16 at 13:57
  • 1
    100 samples is never going to be representative to decide if the probabilities are right or not. – ppeterka Aug 25 '16 at 13:57
  • What should I tell my supervisor who is saying that my generated random numbers are wrong as they are not following the probability ? – statboy Aug 25 '16 at 13:59
  • 3
    What @ppeterka is saying is the distribution 25/0/20 is well within a reasonable variance for 45 samples (i.e. there is a reasonable probability that 45 repeated Bernoulli trials with probability 1/2 give 25 successes and 20 failures, you can calculate it if you want). – Piotr Wilkin Aug 25 '16 at 14:00
  • 2
    "What should I tell my supervisor who is saying that my generated random numbers are wrong as they are not following the probability" Either he doesn't understand how probability works or you don't understand what he's having problems with :) – Piotr Wilkin Aug 25 '16 at 14:00
  • 3
    *cough*that he's wrong *cough*. Your code is correct, though it has unnecessary duplication. That sampling isn't "odd" by any means: You got 45% and 55% sampling, it's close enough for that small sample size. – Diego Martinoia Aug 25 '16 at 14:01
  • @statboy generated random numbers are not wrong. You simulate randomness combined with more randomness. Results may vary. Explain that to him. – f1sh Aug 25 '16 at 14:01
  • You can always convince him easily: run the code for 500/1000 samples (it should be quick, like a second or two) and you'll see you'll have far closer results to the expected distribution. – Diego Martinoia Aug 25 '16 at 14:03
  • Also see https://en.wikipedia.org/wiki/Law_of_large_numbers for the explanation. – Piotr Wilkin Aug 25 '16 at 14:03
  • @DiegoMartinoia ...even a couple of million numbers would not take too long on any reasonable hardware... – ppeterka Aug 25 '16 at 14:05
  • 2
    For a real life counterpart - this is like saying: I flipped a coin 6 times, the coin is most certainly biased because I didn't get heads 3 times. – Piotr Wilkin Aug 25 '16 at 14:05
  • @ppeterka yes, but you don't need that many for your sample average to hit close to the real average :) – Diego Martinoia Aug 25 '16 at 14:11

0 Answers0