Imitation "Wheel of Fortune" (Monte Carlo method or Miss method)

I'm trying to make a randomizer that will use the Monte Carlo Heath method or miss simulation.

I have a Key-Value pair that represents an identifier and a probability value:

ID - Value
2  - 0.37
1 - 0.35
4 - 0.14
3 - 0.12

When you add all these values, you get a total of 1.0.

You can imagine these values ​​as the total “cut” area on the “wheel” (EG: ID 2 takes up 37% of the wheel, and ID 3 takes up only 12% of the wheels). When it is converted to a "range", it will look like this:

ID - Value - Range
2  - 0.37 - 0 to 37
1 - 0.35 - 37 to 72
4 - 0.14 - 72 to 86
3 - 0.12- 86 to 100

Now I use Random.NextDouble () to generate a random value that is between 0.0 and 1.0. This random value will be considered a “spin” on the wheel. Let's say the randomizer returns 0.35, then ID 2 will be selected.

, , ?

+3
5

, 0 - 100 ( ), int[] , , , "" :

int randomID = rangesToIDs[random.nextInt(rangesToIDs.length)];

Btw, , , , , . , .

+4

, D [n], D [i] = (id, p) sum (D [i].p = 0..n-1) == 1.

P [n], , P [i] = (q, id): P [i] = (sum (D [j].p j 0..i), D [j ].id) - .. , (). , P q (.. ).

, , r (0 <= r <= 1)

, P [i].q <= r; P [i].id - .

. , - .

+1

/ "" "" , ,

, , ,

0

jk, .

, :

0,37 2
0,72 1
0,86 4
1,00 3

xx = 0.66.. , ( 0,37) xx < [].key return dict [i].value

Or another solution that comes to my mind is a list of user objects containing lower and upper bounds and values. Then you iterate over the list through the list and check to see if the number is in the range up and down.

0
source
boundaries = [37, 72, 86, 100]
num = 100 * random
for i in boundaries:
  if num < i then return i
0
source

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