我需要一个可移植的,一致的伪随机数生成器 [英] I need a portable, consistent pseudorandom number generator

查看:129
本文介绍了我需要一个可移植的,一致的伪随机数生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写亲子加密函数,我需要一个PRNG,它可以在各个OS上产生一致的结果(因此,无需浮点数学运算,就可以利用硬件或系统级软件).很好,但不是必须的,因为PRNG的周期长于2 30 .

I am writing a kid sister encryption function and I need a PRNG that produces consistent results across OSes (so no floating point math, taking advantage of hardware, or system level software). It would be nice, but not necessary, for the PRNG had a period longer than 230.

我当前使用的是32位 Xorshift :

I am currently using a 32 bit Xorshift:

#!/usr/bin/perl

use strict;
use warnings;

{
    use integer; #use integer math
    my $x = 123456789;
    my $y = 362436069;
    my $w = 88675123; 
    my $z = 521288629;

    sub set_random_seed {
        $w = shift;
    }

    sub random { 
        my $t = $x ^ ($x << 11);
        $x = $y;
        $y = $z;
        $z = $w;
        my $rand = $w = ($w ^ ($w >> 19)) ^ ($t ^ ($t >> 8)); 
        return $rand % 256; #scale it back to a byte at a time
    }
}

set_random_seed(5);
print map { random(), "\n" } 1 .. 10;

但是我很担心,因为我不太了解它是如何工作的.例如,原始源没有设置种子的功能,因此我添加了一个种子,但是我不知道是否为种子选择了正确的变量.

But I am worried because I don't really understand how it works. For example, the original source did not have an ability to set the seed, so I added one, but I don't know if I chose the correct variable for the seed.

所以,所有这些归结为

  1. 您知道CPAN上适合我需要的模块吗?
  2. 如果没有,您知道适合我需求的算法吗?

推荐答案

Math :: Random :: Auto 是一个CPAN模块,实现了众所周知的

Math::Random::Auto is a CPAN module implementing the well-known Mersenne twister PRNG.

这篇关于我需要一个可移植的,一致的伪随机数生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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