admin管理员组文章数量:1277320
I am using the following code to generate a random number:
function getRandomInt (min, max) {
return Math.floor((Math.random() * (max - min + 1)) + min;
}
What I want to do is add a weighting that favours the numbers at the lower end of the range.
I thought about maybe trying to multiply the numbers by 1/cosine.
Would this work and does anyone know how I might go about it?
Many thanks!
I am using the following code to generate a random number:
function getRandomInt (min, max) {
return Math.floor((Math.random() * (max - min + 1)) + min;
}
What I want to do is add a weighting that favours the numbers at the lower end of the range.
I thought about maybe trying to multiply the numbers by 1/cosine.
Would this work and does anyone know how I might go about it?
Many thanks!
Share Improve this question edited Jun 1, 2015 at 2:16 Amit Verma 41.2k21 gold badges97 silver badges118 bronze badges asked Dec 14, 2013 at 18:14 user3102815user3102815 711 silver badge5 bronze badges 5- possible duplicate of Generate A Weighted Random Number – Felix Kling Commented Dec 14, 2013 at 18:16
- if you want a cosine distribution you have to apply the inverse cosine, that means arccos(x) with x being your uniform random sample – Marian Theisen Commented Dec 14, 2013 at 18:16
- 1 what distribution do you want? – Stephen Thomas Commented Dec 14, 2013 at 19:01
- I am not sure how best to explain this. Imagine a logarithmic curve that goes from 0-1. If the RNG returns a value of 0.5 I want to apply a weighting to being it down to say, 0.2. If 1 is returned, I would like to keep the number up at around 1, and similarly 0 with 0. – user3102815 Commented Dec 14, 2013 at 19:24
- Ask a new question. Your "edit" has pletely changed the question. And ask ot on math exchange. – chiliNUT Commented Dec 14, 2013 at 21:04
2 Answers
Reset to default 12First Solution
You need a function which contains the points (0, 0) and (1, 1). For instance: x^n
when n > 0
Math.pow(1, n) === 1
And
Math.pow(0, n) === 0
Therefore, you would just change n
depending on how you want the weighting to work.
- When
n = 1
:y === x
- When
n > 1
:y <= x
- When
0 < n < 1
:y >= x
So, if you want lower values to be favored over higher values, simply use n > 1
.
var weighted = Math.pow(Math.random(), 2);
Then you can scale the result as usual.
var scaled = Math.floor(weighted * (max - min + 1)) + min;
Other Functions
Likewise, you could use any continuous function which contains the points (0, 0), (1, 1), and has range and domain of [0, 1].
Sine
y = sin(xπ/2)
Cosine
y = 1 - cos(xπ/2)
EDIT: there was a type in the final formula, log(2+log(x)) is incorrect it should have been log(1+log(x))+1, its fixed now.
If you are using logarithmic weighting, using something like
var x = Math.random();
var weighted = x * Math.log(1+x);
would make 0.5 weigh in at around 0.2, but 1 would only weigh in at around 0.69.
Using this
var x = Math.random();
var weighted = x * Math.log(2 + Math.log(x));
would allow 1 to weigh in at 1. So bine them, and this
var x = Math.random();
var weighted = (x <= 0.5) ? x * Math.log(1 + x) : x * Math.log(1 + Math.log(x))+1;
should do the trick
本文标签: Weighted Random Number Generator in JavascriptStack Overflow
版权声明:本文标题:Weighted Random Number Generator in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741211051a2359128.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论