From 5 dice rolls generate a random number in the range [1 - 100]

I went through some coding exercises and had some problems with this question:

From 5 cubic (6-sided) rolls, generate a random number in the range [1 - 100].

I implemented the following method, but the return number is not random (called a function 1,000,000 times, and multiple numbers never display in 1-100).

public static int generator() { Random rand = new Random(); int dices = 0; for(int i = 0; i < 5; i++) { dices += rand.nextInt(6) + 1; } int originalStart = 5; int originalEnd = 30; int newStart = 1; int newEnd = 100; double scale = (double) (newEnd - newStart) / (originalEnd - originalStart); return (int) (newStart + ((dices - originalStart) * scale)); } 
+5
source share
3 answers

Good, so 5 dice, each of which has 6 options. if they are not ordered, you have a range of 5-30, as indicated above - it is never enough for 1-100.

You need to accept the order, this gives you a scale of 1,1,1,1,1 - 6,6,6,6,6 (base 6) under the condition 1 โ†’ 0 value, you have a 5-digit base number 6. How we all know 6 ^ 5 = 7776 unique opportunities .;)

For this, I am going to give you a biased random decision.

 int total = 0; int[] diceRolls; for (int roll : diceRolls) { total = total*6 + roll - 1; } return total % 100 + 1; 

thanks to JosEdu for specifying bracket requirements

In addition, if you want to cancel this offset, you can divide the range by maxval specified in my description above, and then multiply by your total (then add the offset), but you still need to determine which rounding rules you used.

+7
source

The problem is that in 5-30 random values โ€‹โ€‹does not exist to display one to one up to 1-100 intervals. This means that certain values โ€‹โ€‹are destined to never appear; the amount of these โ€œlostโ€ values โ€‹โ€‹depends on the ratio of the sizes of the two intervals.

However, you can use the power of your bones in a more efficient way. Here's how I do it:

Approach 1

  • Use the result of the first cube to select one additional interval from 6 equal sub-intervals of size 16.5 (99/6).
  • Use the result of the second cube to select one additional interval from 6 equal subsequences of the interval selected in the first stage.
  • Use ... I think you know what comes next.

Approach 2

Build your random number using numbers in the base-6 system. I.E. The first dice is the first digit of the base-6 number, the second game is the second digit, etc.
Then convert to base-10 and divide by (46656/99). You must have your random number. Of course, in fact, you can only use 3 cubes, the other two are just superfluous.

+1
source

Swapping a 6-sided matrix 5 times leads to 6 ^ 5 = 7776 possible sequences, all the same probable. Ideally, you would like to break these sequences into 100 groups of the same size, and you will have your [1 - 100] rng, but since 7776 is not evenly divided by 100, this is not possible. The best you can do to minimize bias is 76 groups, which are mapped to 78 sequences, each of which is 24 groups, mapped to 77 sequences. Encode (ordered) cubes as base 6, number n, and return 1 + (n% 100).

Not only is there no way to remove an offset with 5 dice, there is no number of dice rolls that completely remove the offset. There is no value of k for which 6 ^ k is evenly divided by 100 (we consider simple decompositions). This does not mean that there is no way to eliminate the displacement, it simply means that you cannot remove the displacement using a procedure that is guaranteed to stop after any specific number of rolls in the bone. But you could, for example, make 3 dice, creating 6 ^ 3 = 216 sequences encoded as the base number 6 n, and return 1 + (n% 100) if n <200. The trap is that if n> = 200, you must repeat the procedure and continue repeating until you get n <200. Thus, there is no prejudice, but there is also no limit to how long you are stuck in the cycle. But since the probability of repetition is only 16/216 each time, from a practical point of view, this is not a very big problem.

+1
source

Source: https://habr.com/ru/post/1201781/


All Articles