将java随机数生成向某个数字倾斜 [英] Skewing java random number generation toward a certain number

查看:136
本文介绍了将java随机数生成向某个数字倾斜的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,如何生成随机数,但使该随机数偏向特定数字。例如,我想生成一个介于1和100之间的数字,但是我希望这个数字倾向于说,75。但是我仍然希望能够获得该范围内的其他数字,但我希望更多地获得数字接近75说,而不是只是在整个范围内得到随机数。谢谢

How, in Java, would you generate a random number but make that random number skewed toward a specific number. For example, I want to generate a number between 1 and 100 inclusive, but I want that number skewed toward say, 75. But I still want the possibility of getting other numbers in the range, but I want more of a change of getting numbers close to say 75 as opposed to just getting random numbers all across the range. Thanks

推荐答案

尝试 http://download.oracle.com/javase/6/docs/api/java/util/Random.html#nextGaussian()

Math.max(1, Math.min(100, (int) 75 + Random.nextGaussian() * stddev)))

选择一个像10这样的stddev并一直玩到你得到你想要的发行版。在1和100处会稍微多于2或99.如果你想改变它下降的速率,你可以将高斯增加到一个幂。

Pick a stddev like 10 and play around until you get the distribution you want. There are going to be slightly more at 1 and 100 though than at 2 or 99. If you want to change the rate at which it drops off, you can raise the gaussian to a power.

这篇关于将java随机数生成向某个数字倾斜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆