B
B
BonBon Slick2017-10-22 18:34:48
IT education
BonBon Slick, 2017-10-22 18:34:48

Generating specific numbers from random ones more often than others?

I got the problem, I decided to share it because I don't know how to solve it :) We
generate it using JS. It is necessary that the numbers that are closer to the min and max ranges fall out N times more often than the central ones.
Example:
Math.floor(Math.random() * (max - min) + min);
This example simply generates a random number from min to max.
1 - It is necessary that min and max fall out more often, say, by 40% than the middle number during generation, that is, if min = 1 and max = 10, then middle = 5, it should fall out least often.
2 - The closer to the min and max ranges, the more often the numbers fall out. That is, the number 9 will fall out more often than the previous ones, which are closer to the middle.
The starting point will be no more than 39% more often, since there we have an upper range, this is 10, a number that must fall out on a level with min more often than the rest.
Simple examples are welcome.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Sokolov, 2017-10-22
@BonBonSlick

Since Math.random()it gives a uniform probability distribution, you can collect numbers with the distribution you need into a large array, and use it Math.random()to pull out the next value from it.
For example, for min=1, max=5 generate var all = [1,1,1,1,2,2,3,4,4,5,5,5,5]and take out from it all[ Math.floor(Math.random()*all.length)]

G
Griboks, 2017-10-22
@Griboks

Two options immediately come to mind:
1) use a special distribution law
2) use a pseudo-random generator. In js conditions it will be pseudo-pseudo-random))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question