-2

Is there a simple algorithm that will print results of rolling a die such that the probability of getting 1,2,3,4,5 is 1/9 and the probability of getting a 6 is 3/9.

I would like to implement this in Java and intentionally only use Math.random(), if statements, for/ while loops.

  • Does this answer your question? [Dice Sum Probability with Different types of Dice](https://stackoverflow.com/questions/29854866/dice-sum-probability-with-different-types-of-dice) – Dennis Kozevnikoff Jan 08 '21 at 20:43
  • Very crudely you could generate a random value in the range 1...9 and return 6 for a value of 7, 8 or 9? Not really an algorithm as such but it's certainly simple. – stridecolossus Jan 08 '21 at 20:43
  • 1
    Probability of getting 6 should be 4/9 to make the sum of all events 1, unless you want the probabilities of 1..5 to be 1/8, then p(6) = 3/8 – Nowhere Man Jan 08 '21 at 20:46
  • The correct probability of 6 is P(6) = 4/9 as you suggested for the sum of all events to be 1. Alex Rundenko, did you come up with anything? – Carmel Monga Jan 08 '21 at 21:14

3 Answers3

1

As others suggested to make the sum of all events equal to 1, then number 9 will have a probability of 4/9 to be chosen.

Generate a random number between 1 and 9, inclusive on both ends. If the number be 1 to 5, you rolled that number, otherwise, you rolled 6. Note that there are 4 chances in this scheme to roll a 6, and 5 total chances to roll 1 through 5.

Random random = new Random();
int roll = random.nextInt(9) + 1; // create a random number

if (roll > 5) {
    System.out.println("You rolled a 6");
}
else {
    System.out.println("You rolled a " + roll);
}

To simulate more dice rolls you can add the above logic inside a for loop that runs for as many loops as you want.

Thanasis M
  • 1,144
  • 7
  • 22
  • 1
    the reasoning is correct. Thanks for the 2 cents – Carmel Monga Jan 08 '21 at 22:35
  • @CarmelMonga glad to help, if this or any answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Thanasis M Jan 20 '21 at 20:07
0

Generating values for random variables with a certain distribution usually works like this:

  • You have a function which generates a random 0 <= q < 1,
  • You apply the quantile function and you obtain the value of your variable.

In your case you have a discrete random variable. You need an instance of Random:

private static final Random random = new Random();

the values assumed by the variable:

private static final double[] values = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};

Compute the cumulative distribution function (sum of probabilities of the values up to the specific value) for these values:

private static final double[] cdf = {1.0 / 9, 2.0 / 9, 3.0 / 9, 4.0 / 9, 5.0 / 9, 1.0};

You random generating function will return the last value for which the cdf is not greater than q:

   public static double randomValue() {
      double q = random.nextDouble();
      for (int i = 0; i < values.length; i++) {
         if (q > cdf[i]) continue;
         return values[i];
      }
      throw new IllegalStateException();
   }
Piotr P. Karwasz
  • 12,857
  • 3
  • 20
  • 43
0

Seems pretty straightforward:

// Pass in an instance of class Random
public static int gen(Random r) { 
  int i = r.nextInt(9); // uniformly generate 0,...,8 inclusive
  if (i < 5) {
    return i + 1;       // returns 1,...,5 w/ probability 1/9
  } else {
    return 6;           // returns 6 w/ probability 4/9
  }
}

Warning, I no longer have a Java compiler on my machine, so I haven't compiled this. However, the algorithm is valid as confirmed in another language.

pjs
  • 18,696
  • 4
  • 27
  • 56