D
D
Damir Abdullin2012-05-20 23:29:20
Python
Damir Abdullin, 2012-05-20 23:29:20

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

5 answer(s)
D
denver, 2012-05-20
@denver

We fill the array with 50, 49, 49, 48, 48, 48, 47, 47, 47, 47 ... and choose a random one from it

S
Sergey Aganezov, 2012-05-21
@Karde

Simulate with rand the uniform on (0,1), and with its help simulate what you already need.

S
skomoroh, 2012-05-21
@skomoroh

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))]]

E
Ents, 2012-05-21
@Ents

<?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);

Here's an example. destination is the integral of the distribution of the required probability probability (normalized to one)
In this case, numbers are returned according to the Maxwell distribution

B
Bashuk, 2012-05-20
@Bashuk

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 question

Ask a Question

731 491 924 answers to any question