比较Java和Matlab中的Mersenne Twister [英] Comparing the Mersenne Twister in Java and matlab

查看:102
本文介绍了比较Java和Matlab中的Mersenne Twister的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在比较Java和Matlab中的mersenne扭曲器.我在两个中都使用相同的种子. 我的问题是,当我从每个数字生成器(分别在Java和Matlab中运行的Mersenne Twister)打印出十个数字时,结果输出似乎不匹配. Matlab版本的输出数据会从Java程序中打印出第二个数字.

I'm comparing the mersenne twister in Java and matlab. I'm using the same seed in both. My problem is that when i print out ten numbers from each number generator (Mersenne Twister running in Java and Matlab respectively), the resulting output doesn't seem to match. The output data from the Matlab version prints out every second number from the program in Java.

Java打印:

0.417,0.997,0.720,0.932,0.0001 ..

0.417, 0.997, 0.720, 0.932, 0.0001..

Matlab打印:

0.417,0.720,0.0001 ..

0.417, 0.720, 0.0001..

任何人都可以指出正确的方向,以找出发生这种情况的原因吗?

Can anyone point me in the right direction to figure out why this happens?

Java:

public class TestRand {
    static MersenneTwister r = new MersenneTwister(1);

    public static void main(String[] args) {

        int ant = 10;
        float[] randt = new float[ant];

        for (int i = 0; i < ant; i++){
            randt[i] = r.nextFloat()*1;
            System.out.println(randt[i]);    
        }
        System.out.println("------------twist");
    }
}

Matlab:

s = RandStream('twister','Seed',1)
RandStream.setGlobalStream(s);

r = 1 .* rand(1,10);

我正在MatLab中使用Mersenne Twister的标准实现,可以找到我正在使用的Java版本

I am using the standard implementation of the Mersenne Twister in MatLab, the Java-version I am using can be found here

推荐答案

Matlab rand()结果是64位double值.但是,您要调用nextFloat()从Java MersenneTwister获取32位值.检查Java源-nextDouble使用的随机性是nextFloat的两倍,并且两次调用next().在您的TestRand Java代码中将nextFloat()替换为nextDouble(),您的结果应该会更好.

The Matlab rand() results are 64-bit double values. But you're calling nextFloat() to get 32-bit values from the Java MersenneTwister. Check that Java source - nextDouble uses twice as much of the randomness as nextFloat, with two calls to next(). Replace nextFloat() with nextDouble() in your TestRand Java code and your results should line up better.

这篇关于比较Java和Matlab中的Mersenne Twister的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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