0

I want to get an integer random number between 0 and 100 with different probabilities in different ranges.

For example I want the probability of values between 0 and 20 to be 0.5 and the probability of values between 21 and 80 to be 0.4 and the probability of values between 81 and 100 to be 0.1.

Is there any method or class in Java or any library for Java to do it? If not, how can I do it myself?

Ivar
  • 6,138
  • 12
  • 49
  • 61
MR.Iraji
  • 56
  • 9
  • Write some code and show us. – nicomp Jun 17 '18 at 11:14
  • No Rafał Sokalski. that post is looking for a way to have different probabilities for different values. but I am looking for a way to have different probabilities for different ranges. – MR.Iraji Jun 17 '18 at 11:15

3 Answers3

2

You just need to have an extra random number determining the range it should generate:

int getRandomNumberWithRangeProbability() {
    double range = Math.random();

    if (range < 0.5) {
        return randomWithRange(0, 20);
    } else if (range < 0.9) {
        return randomWithRange(21, 80);
    } else {
        return randomWithRange(81, 100);
    }
}

int randomWithRange(int min, int max) {
    int range = (max - min) + 1;
    return (int) (Math.random() * range) + min;
}

A small test can be found here.

Credits to AusCBloke for the randomWithRange() method.

Ivar
  • 6,138
  • 12
  • 49
  • 61
1

You should get random in each range and then get another random between 0 and 1 and treat in your interest Good Luck ETG

0

I can think of this way don't know if there is any inbuilt function for doing this or not

  1. So Make a function that will return random between two integers.
  2. make a variable probable having random value of 1-10
  3. Satisfy these condition
if(probable>=0 &&  probable<=5){
          random = getUniqueRandom(0, 20);

      }
      else if(probable>=6 &&  probable<=9) {
          random = getUniqueRandom(21, 80);

      }
      else if (probable == 10) {
          random = getUniqueRandom(81, 100);

      }

Here is the working implementation

import java.util.Random;


public class Solution  {
    private static Random r = new Random();
    public static void main(String[] args) {
        int pro1 = 0, pro2 =0, pro3 =0;
        for(int i=0; i<10000; i++) {
        int probable = getUniqueRandom(0, 10);
        int random = 0;

        if(probable>=0 &&  probable<=5){
            random = getUniqueRandom(0, 20);
            pro1++;
        }
        else if(probable>=6 &&  probable<=9) {
            random = getUniqueRandom(21, 80);
            pro2++;
        }
        else if (probable == 10) {
            random = getUniqueRandom(81, 100);
            pro3++;
        }

        //System.out.println(random);
    }
        System.out.println("Checked 10000 Times.\n0-20 Found: "+pro1);
        System.out.println("21-80 Found: "+pro2);
        System.out.println("81-100 Found: "+pro3);
    }
    static int getUniqueRandom(int min, int max){
        int num = r.nextInt(max-min+1) + min;
        return num;
    }
}
Sundeep
  • 452
  • 2
  • 12