I need a portable, consistent pseudorandom number generator

我的梦境 提交于 2019-12-04 07:08:43
unwind

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

Try using an LFSR - Linear Feedback Shift Register.. The first link on the external links has everything you need to produce any number of bits of randomness. The nice thing about this is that it is easy to implement and can be done using all integer math.

I've used it with success on an 8051 project. With perl it will be a snap.

Update:

Here's a quick perl implementation of an 8 bit LFSR:

use strict;
use warnings;

use List::Util qw(reduce);
use vars qw($a $b);

print 'Taps: ', set_taps( 8, 7, 2, 1 ), "\n";
print 'Seed: ', seed_lfsr( 1 ), "\n";
print read_lfsr(), "\n" for 1..10;

BEGIN {
    my $tap_mask;
    my $lfsr = 0;

    sub read_lfsr {
        $lfsr = ($lfsr >> 1) ^ (-($lfsr & 1) & $tap_mask );

        return $lfsr;
    }

    sub seed_lfsr {
        $lfsr = shift || 0;
        $lfsr &= 0xFF;
    }

    sub set_taps {
        my @taps = @_;

        $tap_mask = reduce { $a + 2**$b } 0, @taps;

        $tap_mask >>= 1;

        $tap_mask &= 0xFF;

        return $tap_mask;
    }
}

This code is just a prototype. If I wanted to use it in production I'd probably wrap it in a object and make register size configurable. Then we'd be rid of those pesky shared variables.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!