aboutsummaryrefslogtreecommitdiff
path: root/tests/ii/Random.pm
diff options
context:
space:
mode:
Diffstat (limited to 'tests/ii/Random.pm')
-rw-r--r--tests/ii/Random.pm25
1 files changed, 25 insertions, 0 deletions
diff --git a/tests/ii/Random.pm b/tests/ii/Random.pm
new file mode 100644
index 0000000..a7a95e5
--- /dev/null
+++ b/tests/ii/Random.pm
@@ -0,0 +1,25 @@
+package Random;
+use strict;
+use warnings;
+use Digest::SHA qw(sha256);
+
+# Fixed byte order and decimal counter: independent of Perl's rand and hash seed.
+sub new {
+ my ($class, $seed, $group) = @_;
+ return bless { seed => $seed, group => $group, counter => 0 }, $class;
+}
+sub integer {
+ my ($self, $low, $high) = @_;
+ die "invalid random range\n" if $high < $low || $high - $low >= 4294967296;
+ my $bytes = sha256($self->{seed} . "\0" . $self->{group} . "\0" . $self->{counter}++);
+ return $low + unpack('N', substr($bytes, 0, 4)) % ($high - $low + 1);
+}
+sub emit {
+ my ($self, $count, $edges, $make) = @_;
+ die "positive count required\n" unless defined($count) && $count =~ /^[1-9][0-9]*$/;
+ binmode STDOUT; # Identical input bytes on every platform.
+ print scalar(@$edges) + $count, "\n";
+ print "$_\n" for @$edges;
+ print $make->(), "\n" for 1 .. $count;
+}
+1;