且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将java随机数生成向某个数字倾斜

更新时间:2023-02-10 12:13:44

尝试 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.