将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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题有点老了,但是如果有人想在不进行特殊情况处理的情况下执行此操作,您可以使用如下函数:
参数执行以下操作:
Question is a bit old, but if anyone wants to do this without the special case handling, you can use a function like this:
The parameters do the following:
尝试 http://download.oracle。 com/javase/6/docs/api/java/util/Random.html#nextGaussian()
选择一个像 10 这样的 stddev 并进行尝试,直到获得所需的分布。 1 和 100 处的值会比 2 或 99 处的值稍微多一些。如果你想改变它下降的速率,你可以将高斯提高到一个幂。
Try http://download.oracle.com/javase/6/docs/api/java/util/Random.html#nextGaussian()
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.