Forward-leaning random numbers' probability distribution
Say I'm pseudo-randomly picking a number from 1 to 50 every second for 100 seconds, and as time goes on the number picked is more likely to be greater. How cou开发者_如何学编程ld I structure such an algorithm?
For example: after 99 seconds the probability of choosing a number closer to 50 is much more likely than choosing a number closer to 1.
Or: the number picked after 10 seconds is more likely to be greater than the number picked after 9 seconds
Pick any concave monotonic function like square root which maps 0 to 0 and 1 to 1. Generate a random number between [0,1], apply the function and then scretch [0,1] to the desired interval ([1,50]).
Now if you morph from the linear transformation f(x)=x to the mentioned transform function with for example a simple weighting you have the desired effect.
I have a simple solution for you. Instead of rand(1, 50)
(say this function generates uniformly random numbers 1..50) use this expression:
power(rand(1, power(50, exp)), 1/exp)
this will still give you all the numbers 1..50. For exp = 1
, the distribution will be uniform. As you slightly increase exp (e.g. like 1.1 or so), the probability of getting larger numbers will increase. The higher the exp, the more it will increase towards 50.
So you can do e.g.:
factor = 1 /* finetune this for your needs */
for second = 0..100
exp = 1 + (second / 100) * factor
rand_num = power(rand(1, power(50, exp)), 1/exp)
endfor
Pseudocode:
let i = 0
let n = 50 // Adjust for your needs
for i goes to 100 {
randomnum = int(sqrt(rand(1, 50*n)));
}
This can be very forward-leaning, but it's one way to approach it.
Thanks to Ricky Bobby for pointing out a fundamental problem with my old approach. This is inspired by yi_H's suggestion of using a function like sqrt.
There are probably simpler ways for what you're doing, but the general solution is to use inverse transform sampling.
Essentially, if you want to produce a random number with a given PDF, p(x), you first calculate the inverse cumulative density function (CDF), P'(x). You can then generate uniform random numbers between 0 and 1, and then apply P'(x) to them.
精彩评论