函数返回指数范围为(a,b)的随机双精度数 [英] Function returning random double with exponential distribution in range (a,b)

查看:139
本文介绍了函数返回指数范围为(a,b)的随机双精度数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成一个随机数,从 a b 。问题是,数字必须以指数分布给出。

I want to generate a random number from a to b. The problem is, the number has to be given with exponential distribution.

这是我的代码:

public double getDouble(double low, double high)
        {
            double r;
            (..some stuff..)
            r = rand.NextDouble();
            if (r == 0) r += 0.00001;
            return (1 / -0.9) * Math.Log(1 - r) * (high - low) + low;
        }

问题是(1 / -0.9)* Math.Log(1 -r)不在0到1之间,因此结果不会在 a b 之间。有人可以帮忙吗?

The problem is that (1 / -0.9) * Math.Log(1 - r) is not between 0 and 1, so the result won't be between a and b. Can someone help? Thanks in advance!

推荐答案

在第一个答案中我误解了您的问题:)您已经在使用反转采样。

I missunderstood your question in the first answer :) You are already using the inversion sampling.

要将一个范围映射到另一个范围,有 一种典型的数学方法:

To map a range into another range, there is a typical mathematical approach:

f(x) = (b-a)(x - min)/(max-min) + a

其中

b = upper bound of target
a = lower bound of target
min = lower bound of source
max = upper bound of source
x = the value to map

(这是线性缩放,因此将保留分布)

(this is linear scaling, so the distribution would be preserved)

(您可以验证:如果放入 min 表示 x ,如果放入<$,则结果为 a c $ c> max 为 x ,您将获得 b 。)

(You can verify: If you put in min for x, it results in a, if you put in max for x, you'll get b.)

现在的问题:指数分布的最大值为 inf 。因此,您不能使用此等式,因为无论如何,它总是 / inf + 0 -因此, 0 。 (从数学上讲这是有道理的,但ofc。不能满足您的需求)

The Problem now: The exponential distribution has a maximum value of inf. So, you cannot use this equation, because it always wold be whatever / inf + 0 - so 0. (Which makes sense mathematically, but ofc. does not fit your needs)

因此,唯一正确的答案是:有两个固定数字之间没有指数分布,因为您无法映射[0,inf]-> [a,b]

So, the ONLY correct answer is: There is no exponential distribution possible between two fixed numbers, cause you can't map [0,inf] -> [a,b]

因此您需要一些权衡取舍,以使您的结果尽可能地成为指数

Therefore you need some sort of trade-off, to make your result as exponential as possible.

出于好奇,我将头转向各种可能性,我发现简直无法抗拒的数学:P

I wrapped my head around different possibilities out of curiosity and I found that you simple can't beat maths on this :P

但是,我用Excel和140万条随机记录做了一些测试:
我选择了一个随机数作为极限(10),并将计算结果四舍五入到小数点后一位。 (0、0.1、0.2,依此类推)我用这个数字执行线性转换,最大为10,将大于1的任何结果归并。

However, I did some test with Excel and 1.4 Million random records: I picked a random number as "limit" (10) and rounded the computed result to 1 decimal place. (0, 0.1, 0.2 and so on) This number I used to perform the linear transformation with an maximum of 10, ingoring any result greater than 1.

1.4数百万次计算(生成10-20次),仅生成了7-10个大于1的随机数:

Out of 1.4 Million computations (generated it 10-20 times), only 7-10 random numbers greater than 1 have been generated:

(概率密度函数,将值映射后:列100:= 1,列0:= 0)

(Probability density function, After mapping the values: Column 100 := 1, Column 0 := 0)

所以:


  • 使用上述线性方法将值映射到 [0,1] ,假定最大值为

  • 如果在转换后遇到一个大于1的值-只需绘制另一个随机数,直到该值<等于10。 1。

  • Map the values to [0,1], using the linear approach mentioned above, assume a maximum of 10 for the transformation.
  • If you encounter a value > 1 after the transformation - just draw another random number, until the value is < 1.

在140万次测试中只有7-10次出现,这应该足够接近,因为重新绘制的数字将再次是伪指数分布的

With only 7-10 occurences out of 1.4 Million tests, this should be close enough, since the re-drawn number will again be pseudo-exponential-distributed.

这篇关于函数返回指数范围为(a,b)的随机双精度数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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