将java随机数生成偏向某个数字

发布于 2024-11-04 04:00:00 字数 139 浏览 2 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少年亿悲伤 2024-11-11 04:00:00

问题有点老了,但是如果有人想在不进行特殊情况处理的情况下执行此操作,您可以使用如下函数:

    final static public Random RANDOM = new Random(System.currentTimeMillis());

    static public double nextSkewedBoundedDouble(double min, double max, double skew, double bias) {
        double range = max - min;
        double mid = min + range / 2.0;
        double unitGaussian = RANDOM.nextGaussian();
        double biasFactor = Math.exp(bias);
        double retval = mid+(range*(biasFactor/(biasFactor+Math.exp(-unitGaussian/skew))-0.5));
        return retval;
    }

参数执行以下操作:

  • min - 可能的最小偏斜值
  • max - 可能的最大偏斜值
  • skew - 参数值围绕分布模式聚集的程度;较高的值意味着更严格的聚类
  • 偏差 - 模式接近最小值、最大值或中点值的趋势;正值偏向最大值,负值偏向最小值

Question is a bit old, but if anyone wants to do this without the special case handling, you can use a function like this:

    final static public Random RANDOM = new Random(System.currentTimeMillis());

    static public double nextSkewedBoundedDouble(double min, double max, double skew, double bias) {
        double range = max - min;
        double mid = min + range / 2.0;
        double unitGaussian = RANDOM.nextGaussian();
        double biasFactor = Math.exp(bias);
        double retval = mid+(range*(biasFactor/(biasFactor+Math.exp(-unitGaussian/skew))-0.5));
        return retval;
    }

The parameters do the following:

  • min - the minimum skewed value possible
  • max - the maximum skewed value possible
  • skew - the degree to which the values cluster around the mode of the distribution; higher values mean tighter clustering
  • bias - the tendency of the mode to approach the min, max or midpoint value; positive values bias toward max, negative values toward min
浊酒尽余欢 2024-11-11 04:00:00

尝试 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 处的值稍微多一些。如果你想改变它下降的速率,你可以将高斯提高到一个幂。

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文