随机数Mathematica vs Java

随机数Mathematica vs Java,java,random,wolfram-mathematica,Java,Random,Wolfram Mathematica,哪一组更“随机” random()用于Java还是random用于Mathematica?Java是蓝色的,Mathematica是红色的 数字从0到50(51?) 编辑: 这是用Mathematica生成的直方图 Java源代码(丑陋) Mathematica源代码(output.txt是来自Java的转储文件) [编辑]:我编写代码时只是在学习循环。很抱歉给你带来了困惑。现在,我制作了一个更简洁的版本,它们的分布大致相同。我想任意的循环结束会带来很大的不同 新代码: public clas

哪一组更“随机”

random()用于Java还是random用于Mathematica?Java是蓝色的,Mathematica是红色的

数字从0到50(51?)

编辑: 这是用Mathematica生成的直方图

Java源代码(丑陋)

Mathematica源代码(output.txt是来自Java的转储文件)

[编辑]:我编写代码时只是在学习循环。很抱歉给你带来了困惑。现在,我制作了一个更简洁的版本,它们的分布大致相同。我想任意的循环结束会带来很大的不同

新代码:

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;
        }
     }
  }
}
公共类随机数{
公共静态void main(字符串[]args){
整数计数=0;

对于(int i=0;i看一看。它处理java.util.Random并显示一些gotcha。如果您想要真正的er(:-)随机性,它还建议使用(更昂贵,更安全)。

看一看。它处理java.util.Random并显示一些gotcha。如果您想要真正的er(:-),它还建议使用(更昂贵,更安全))随机性。

我发现一个非常平坦的分布,假设它是随机的

下面的代码打印了我希望看到的内容,即由于随机性而导致的事件计数的变化

Random : min count 933, max count 1089
Random : min count 952, max count 1071
Random : min count 922, max count 1056
Random : min count 936, max count 1083
Random : min count 938, max count 1063
SecureRandom : min count 931, max count 1069
SecureRandom : min count 956, max count 1070
SecureRandom : min count 938, max count 1061
SecureRandom : min count 958, max count 1100
SecureRandom : min count 929, max count 1068
/dev/urandom: min count 937, max count 1093
/dev/urandom: min count 936, max count 1063
/dev/urandom: min count 931, max count 1069
/dev/urandom: min count 941, max count 1068
/dev/urandom: min count 931, max count 1080
代码

import java.io.*;
导入java.security.SecureRandom;
导入java.util.Random;
公共班机{
公共静态void main(字符串…参数)引发IOException{
testRandom(“Random”,newrandom());
testRandom(“SecureRandom”,新的SecureRandom());
testRandom(“/dev/uradom”,新的DevRandom());
}
私有静态void testRandom(字符串描述,随机){
对于(int n=0;n<5;n++){
int[]计数=新的int[50];
对于(int i=0;i<50*1000;i++)
计数[随机.nextInt(50)]+;
int min=Integer.MAX\u值,MAX=Integer.min\u值;
用于(整数计数:计数){
如果(最小>计数)最小=计数;
如果(最大值<计数)最大值=计数;
}
系统输出打印项次(描述+”:最小计数“+min+”,最大计数“+max);
}
}
静态类DevRandom扩展了随机{
数据输入流fis;
公共德夫兰多姆(){
试一试{
fis=新的DataInputStream(新的BufferedInputStream(新的FileInputStream(“/dev/uradom”));
}catch(filenotfounde异常){
抛出新断言错误(e);
}
}
@凌驾
受保护的整数下一个(整数位){
试一试{
返回fis.readInt();
}捕获(IOE异常){
抛出新断言错误(e);
}
}
@凌驾
受保护的void finalize()抛出可丢弃的{
super.finalize();
如果(fis!=null)fis.close();
}
} 
}

我发现一个非常平坦的分布,假设它是随机的

下面的代码打印了我希望看到的内容,即由于随机性而导致的事件计数的变化

Random : min count 933, max count 1089
Random : min count 952, max count 1071
Random : min count 922, max count 1056
Random : min count 936, max count 1083
Random : min count 938, max count 1063
SecureRandom : min count 931, max count 1069
SecureRandom : min count 956, max count 1070
SecureRandom : min count 938, max count 1061
SecureRandom : min count 958, max count 1100
SecureRandom : min count 929, max count 1068
/dev/urandom: min count 937, max count 1093
/dev/urandom: min count 936, max count 1063
/dev/urandom: min count 931, max count 1069
/dev/urandom: min count 941, max count 1068
/dev/urandom: min count 931, max count 1080
代码

import java.io.*;
导入java.security.SecureRandom;
导入java.util.Random;
公共班机{
公共静态void main(字符串…参数)引发IOException{
testRandom(“Random”,newrandom());
testRandom(“SecureRandom”,新的SecureRandom());
testRandom(“/dev/uradom”,新的DevRandom());
}
私有静态void testRandom(字符串描述,随机){
对于(int n=0;n<5;n++){
int[]计数=新的int[50];
对于(int i=0;i<50*1000;i++)
计数[随机.nextInt(50)]+;
int min=Integer.MAX\u值,MAX=Integer.min\u值;
用于(整数计数:计数){
如果(最小>计数)最小=计数;
如果(最大值<计数)最大值=计数;
}
系统输出打印项次(描述+”:最小计数“+min+”,最大计数“+max);
}
}
静态类DevRandom扩展了随机{
数据输入流fis;
公共德夫兰多姆(){
试一试{
fis=新的DataInputStream(新的BufferedInputStream(新的FileInputStream(“/dev/uradom”));
}catch(filenotfounde异常){
抛出新断言错误(e);
}
}
@凌驾
受保护的整数下一个(整数位){
试一试{
返回fis.readInt();
}捕获(IOE异常){
抛出新断言错误(e);
}
}
@凌驾
受保护的void finalize()抛出可丢弃的{
super.finalize();
如果(fis!=null)fis.close();
}
} 
}

如果这个图对我有什么启示的话,那就是Mathematica的均匀随机分布的质量比您在
Java
中展示的实现要好得多(对于任何Java实现,我都不这么认为。另外,作为免责声明,我也不想发动一场火焰之战,我作为J2EE和Mathematica开发人员已经有一段时间了,尽管我承认在后者方面有更多的经验)

这里是论点。你有50000点和50个箱子(直方图条)如图所示,这表明每个料仓大约有1000个点。更准确地说,我们可以使用遍历性将50000个均匀分布点的问题转化为50000个独立试验的问题,并询问每个料仓的平均点数和方差。任何特定料仓最终精确 然后,通过二项分布给出
Npoints
中的k
点:

其中,平均值是
Npoints/Nbins
(这当然是我们直觉上期望的),方差是
Npoints*(1-1/Nbins)*1/Nbins~Npoints/Nbins=1000
,在我们的例子中(
Npoints=50000,Nbins=50
)。取平方根,我们得到的标准偏差为
sqrt(1000)~32,这是一个
Random : min count 933, max count 1089
Random : min count 952, max count 1071
Random : min count 922, max count 1056
Random : min count 936, max count 1083
Random : min count 938, max count 1063
SecureRandom : min count 931, max count 1069
SecureRandom : min count 956, max count 1070
SecureRandom : min count 938, max count 1061
SecureRandom : min count 958, max count 1100
SecureRandom : min count 929, max count 1068
/dev/urandom: min count 937, max count 1093
/dev/urandom: min count 936, max count 1063
/dev/urandom: min count 931, max count 1069
/dev/urandom: min count 941, max count 1068
/dev/urandom: min count 931, max count 1080
import java.io.*; 
import java.security.SecureRandom; 
import java.util.Random;

public class Main {
    public static void main(String... args) throws IOException {
        testRandom("Random ", new Random());
        testRandom("SecureRandom ", new SecureRandom());
        testRandom("/dev/urandom", new DevRandom());
    }

    private static void testRandom(String desc, Random random) {
        for (int n = 0; n < 5; n++) {
            int[] counts = new int[50];
            for (int i = 0; i < 50*1000; i++)
                counts[random.nextInt(50)]++;
            int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
            for (int count : counts) {
                if (min > count) min = count;
                if (max < count) max = count;
            }
            System.out.println(desc+": min count " + min + ", max count " + max);
        }
    }

    static class DevRandom extends Random {
        DataInputStream fis;

        public DevRandom() {
            try {
                fis = new DataInputStream(new BufferedInputStream(new FileInputStream("/dev/urandom")));
            } catch (FileNotFoundException e) {
                throw new AssertionError(e);
            }
        }

        @Override
        protected int next(int bits) {
            try {
                return fis.readInt();
            } catch (IOException e) {
                throw new AssertionError(e);
            }
        }

        @Override
        protected void finalize() throws Throwable {
            super.finalize();
            if (fis != null) fis.close();
        }
    } 
 }
Needs["JLink`"]

(* Point to a different JVM if you like... *)
(* ReinstallJava[CommandLine -> "...", ClassPath-> "..."] *)

ClearAll@JRandomInteger
JRandomInteger[max_, n_:1] := JRandomInteger[{0, max}, n]
JRandomInteger[{min_, max_}, n_:1] :=
  JavaBlock[
    Module[{range, random}
    , range = max - min + 1
    ; random = JavaNew["java.util.Random"]
    ; Table[min + random@nextInt[range], {n}]
    ]
  ]

Histogram[
  Through[{JRandomInteger, RandomInteger}[{0, 50}, 50000]]
, ChartLegends->{"Java","Mathematica"}
]
public static void main(String[] args) {
    String randomNumberList = " ";
    for (int c = 0; c < 50000; ++c) {
        // random integer in the range 0 <= i < 50
        int i = (int) Math.floor(50 * Math.random());
        System.out.print(i + " ");
    }
}
                 *      *                         
  **   * * ** * ***  ** ***  ** *   *  * *   **** 
****** **************** **************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
#!/usr/bin/env python
s = raw_input()
nums = [int(i) for i in s.split()]
bins = dict((n,0) for n in range(50))
for i in nums:
    bins[i] += 1

import itertools
heightdivisor = 50 # tweak this to make the graph taller/shorter
xx = ['*'*(v / heightdivisor) for k,v in bins.items()]
print '\n'.join(reversed([''.join(x) for x in itertools.izip_longest(*xx, fillvalue=' ')]))