问题
I want to craft an algorithm that will give me numbers that are random in the sense that I have no idea what they'll be, but at the same time, numbers that are closer to 0
have to be more likely to occur as output while those closer to 1
must be less likely. I'd like to play around with both linear and exponential distributions, so please give at least hints for implementing both.
I've thought and thought about how to approach this issue, but I still don't even have a clue, so any pointers would be appreciated.
NOTE: I'm not looking to discuss, nor do I yet understand, the intricacies of "true" vs. "pseudo" randomness... This has nothing to do with security or cryptography, and for it I'll simply be using Javascript's Math.random()
as a seed, just so we're all clear about what I'm asking.
回答1:
var random = Math.pow(Math.random(), 2);
回答2:
Take a look at Poisson distribution, probably you can use it for your own purposes, essentially a poisson distribution is not deterministic, but it has a certain frequency of occurrence: wikipedia has a good introductory info about this: http://en.wikipedia.org/wiki/Poisson_distribution
Algorithm:
algorithm poisson random number (Knuth):
init:
Let L ← e−λ, k ← 0 and p ← 1.
do:
k ← k + 1.
Generate uniform random number u in [0,1] and let p ← p × u.
while p > L.
return k − 1.
来源:https://stackoverflow.com/questions/13451236/how-can-i-distort-the-distribution-of-a-set-of-random-numbers