【发布时间】:2011-02-12 09:45:04
【问题描述】:
哪个集合更“随机”?
Math.random() 用于 Java 还是 random 用于 Mathematica? Java 为蓝色,Mathematica 为红色。
数字从 0 到 50(51?)
编辑: 这是在 Mathematica 中生成的直方图。
Java源码(丑)
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 0;
int sum = 0;
int counter = 0;
String randomNumberList = " ";
int c = 0;
while (c != 50){
while (i != 7) {
i = (int) (51 * Math.random());
sum += i;
++counter;
randomNumberList += " " + i;
}
i = 0;
System.out.print("\n" + randomNumberList);
++c;
}
}
Mathematica 源代码(output.txt 是 Java 的转储文件)
dataset = ReadList["~/Desktop/output.txt", Number]
dataset2 = RandomReal [{0, 50}, 50000]
Histogram[{dataset, dataset2}]
[编辑]:我在编写代码时只是在学习循环。对困惑感到抱歉。现在我制作了一个更简洁的版本,它们的分布大致相同。我猜任意循环结束会产生很大的不同。
新代码:
public class RandomNums {
public static void main(String[] args) {
int count = 0;
for (int i = 0; i <= 50000; i++){
int j = (int) (50 * Math.random());
System.out.print(j + " ");
count++;
if (count == 50){
System.out.println("\n");
count = 0;
}
}
}
}
【问题讨论】:
-
您将不得不解释更多关于该图表所代表的内容。看起来它可能是一个直方图,但很难说。请附上源代码。
-
你是如何生成数字的?
-
随机性的“质量”需要的不仅仅是直方图。您必须查看生成的各种系列。可能有整本关于它的书。
-
@Gabe:说得好!作为一个简单的例子,我可以简单地使用递增的模计数器生成一个具有完全平坦分布的序列。
-
你为什么有
while(i!=7)?我不知道您的 Java 代码生成了多少个数字。为什么不在一个循环中生成 50000 个随机数?
标签: java random wolfram-mathematica