Answer the question
In order to leave comments, you need to log in
Picking a random value
Hello! There is a sequential list of values, for example from 12 to 50. You must choose a random value from this list, provided that the smaller the number, the greater the chance of it falling out. It is desirable that the chance of each subsequent number falling out decreases by the same certain value relative to the previous number. What algorithms would you recommend to solve this problem?
Answer the question
In order to leave comments, you need to log in
We fill the array with 50, 49, 49, 48, 48, 48, 47, 47, 47, 47 ... and choose a random one from it
Simulate with rand the uniform on (0,1), and with its help simulate what you already need.
import random
import bisect
# наш список
l = list(range(12,50))
# задаем веса, например: вес=70-позиция, можно любой
lw = [(i, 70-i) for i in l]
# суммируем веса
tw = 0
wd = dict()
for i, w in lw:
tw += w
wd[tw] = i
wk = sorted(wd.keys())
# получаем наше значение
value = wd[wk[(bisect.bisect_right(wk, random.random()*tw))]]
<?php
function destination($x)
{
return 1/2 * exp(-$x * $x); //интеграл от распределения вероятности x * exp(-x * x), принимает на вход 0..1
}
function my_rand($min, $max)
{
return $min + destination(random()) * ($max - $min);
}
function random() //random 0..1
{
return mt_rand() / mt_getrandmax();
}
echo my_rand(10, 100);
The standard rand() returns, usually, a uniformly distributed random value. But there are formulas for converting them to random variables with other common distributions.
I've heard of the Box-Muller transform, but it takes a pair of uniforms into a pair of normals. Try looking for something similar for exponential e.g.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question