[-1,+1]中两种随机数选择方法比较
Comparing two methods of choosing random numbers in [-1, +1]
我正在参加在线 java 课程并遇到以下挑战:
Compute an estimate of π by simulating throwing darts onto the unit square and determining the fraction of them that lie in the unit circle.
我关于如何生成随机 x 坐标的想法是在 [0, 1] 范围内选择两个均匀随机数,然后将它们相减。结果值的范围从 -1 到 +1。
讲师的答案是将生成的双精度数乘以 2 再减去 1。这是他们的代码:
public class MonteCarlo
{
public static void main(String[] args)
{
System.out.println("Number of tries");
Random generator = new Random(42);
Scanner in = new Scanner(System.in);
int tries = in.nextInt();
int hits = 0;
for (int i = 1; i <= tries; i++)
{
// Generate two random numbers between -1 and 1
double x = generator.nextDouble() * 2 -1 ;
double y = generator.nextDouble() * 2 -1 ;
// Check whether the point lies in the unit circle
if (Math.sqrt((x - 0) * (x - 0) + (y - 0) * (y - 0)) <= 1)
{
hits++;
}
}
/*
The ratio hits / tries is approximately the same as the ratio
circle area / square area = pi / 4
*/
double piEstimate = 4.0 * hits / tries;
System.out.println("Estimate for pi: " + piEstimate);
}
}
我的问题是,我的导师的方法和我的方法有何不同?我的想法行得通吗?
这实际上更像是一道数学题,而不是编程题。您的目标是对 [-1, 1] 范围内的均匀随机实数进行采样,您有两种方法可以做到这一点:
取一个[0, 1]范围内的实数,乘以二,再减一。
对[0, 1]范围内的两个实数均匀采样,然后相减。
方法(1)正确。方法(2)没有。与其解释范围 [0, 1],不如想象一下您想要通过掷两个公平的骰子并从第二个骰子中减去第一个骰子来选择一个介于 -5 和 +5 之间的随机数。尽管每次掷骰子都是公平的并且从 {1, 2, 3, 4, 5, 6} 中均匀地挑选出一些东西,但最终的分布并不均匀。例如:
- 获得 -5 的几率是三十六分之一,因为它只有一种可能的发生方式:您必须在第一个骰子上掷出 1,在第二个骰子上掷出 6。
- 得到 0 的几率是六分之一,因为有六种不同的掷骰结果(1/1、2/2、3/3、4/4、5/5 和 6/6) .
相同的数学论证可以解释为什么减去两个均匀随机数不会在您想要的范围内得到均匀随机分布。相反,您会得到一个三角形分布,该分布在 0 处达到峰值,并随着您向 +1 和 -1 移动而下降。
所以换句话说,你确实得到了一个随机数 - 它不等同于以均匀概率向单位正方形投掷飞镖。
我正在参加在线 java 课程并遇到以下挑战:
Compute an estimate of π by simulating throwing darts onto the unit square and determining the fraction of them that lie in the unit circle.
我关于如何生成随机 x 坐标的想法是在 [0, 1] 范围内选择两个均匀随机数,然后将它们相减。结果值的范围从 -1 到 +1。
讲师的答案是将生成的双精度数乘以 2 再减去 1。这是他们的代码:
public class MonteCarlo
{
public static void main(String[] args)
{
System.out.println("Number of tries");
Random generator = new Random(42);
Scanner in = new Scanner(System.in);
int tries = in.nextInt();
int hits = 0;
for (int i = 1; i <= tries; i++)
{
// Generate two random numbers between -1 and 1
double x = generator.nextDouble() * 2 -1 ;
double y = generator.nextDouble() * 2 -1 ;
// Check whether the point lies in the unit circle
if (Math.sqrt((x - 0) * (x - 0) + (y - 0) * (y - 0)) <= 1)
{
hits++;
}
}
/*
The ratio hits / tries is approximately the same as the ratio
circle area / square area = pi / 4
*/
double piEstimate = 4.0 * hits / tries;
System.out.println("Estimate for pi: " + piEstimate);
}
}
我的问题是,我的导师的方法和我的方法有何不同?我的想法行得通吗?
这实际上更像是一道数学题,而不是编程题。您的目标是对 [-1, 1] 范围内的均匀随机实数进行采样,您有两种方法可以做到这一点:
取一个[0, 1]范围内的实数,乘以二,再减一。
对[0, 1]范围内的两个实数均匀采样,然后相减。
方法(1)正确。方法(2)没有。与其解释范围 [0, 1],不如想象一下您想要通过掷两个公平的骰子并从第二个骰子中减去第一个骰子来选择一个介于 -5 和 +5 之间的随机数。尽管每次掷骰子都是公平的并且从 {1, 2, 3, 4, 5, 6} 中均匀地挑选出一些东西,但最终的分布并不均匀。例如:
- 获得 -5 的几率是三十六分之一,因为它只有一种可能的发生方式:您必须在第一个骰子上掷出 1,在第二个骰子上掷出 6。
- 得到 0 的几率是六分之一,因为有六种不同的掷骰结果(1/1、2/2、3/3、4/4、5/5 和 6/6) .
相同的数学论证可以解释为什么减去两个均匀随机数不会在您想要的范围内得到均匀随机分布。相反,您会得到一个三角形分布,该分布在 0 处达到峰值,并随着您向 +1 和 -1 移动而下降。
所以换句话说,你确实得到了一个随机数 - 它不等同于以均匀概率向单位正方形投掷飞镖。