M
M
Mir2017-08-27 21:26:51
Mathematics
Mir, 2017-08-27 21:26:51

Random number with given probability, what algorithm?

We need an algorithm, or at least a prerequisite for it
Purpose:
given from 0 to N numbers, N can be 3-20
at the output we get a random number in a given range, and if we do
1000 iterations, we get that there are many numbers closer to 0 times more numbers that lie closer to N.
for example, from 0 to 8
, then with 100 or 1000 iterations we get:
0 - 22%
1- 19%
2 - 16%
3 - 13%
4 - 10%
5 - 8%
6 - 6%
7 - 4%
8 - 2%
*numbers are approximate, only to convey the essence of the idea

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Sokolov, 2017-08-27
@mir546

Make a long range, made up of segments proportional to the probabilities.

0..22 : "0"
22..22+19=41: "1"
41..41+16=57: "2"
...
..100: "8"

Choosing from it with linear probability will give the desired distribution.
For example, rolled 56 - falls within the range related to "2"

R
res2001, 2017-08-28
@res2001

This is called the "normal distribution". Read the wiki on this topic. It's a pretty good article.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question