Commits

Shlomi Fish committed b1413a5

Add more.

  • Participants
  • Parent commits eda9d20

Comments (0)

Files changed (1)

gen-shlomif-at-iglu-sig

 use strict;
 use warnings;
 
+use Math::BigInt (lib => 'GMP');
+
 use utf8;
 
 use IO::All;
 
 use File::Spec::Functions qw( catpath splitpath rel2abs );
 
+sub _myrand
+{
+    my ($max) = @_;
+
+    if (open my $fh, '<:raw', '/dev/urandom')
+    {
+        my $n = Math::BigInt->new(0);
+
+        my $buf = '';
+        read($fh, $buf, 16);
+
+        for my $c (split//,$buf)
+        {
+            $n = (($n << 8) | ord($c));
+        }
+
+        close($fh);
+        return ($n % $max);
+    }
+    else
+    {
+        return int( rand($max) );
+    }
+}
+
+sub _pick
+{
+    my ($aref) = @_;
+
+    return $aref->[_myrand(scalar(@$aref))];
+}
+
+
 my $script_dir = catpath( ( splitpath( rel2abs $0 ) )[ 0, 1 ] );
 
 my $sig = io()->file($ENV{HOME}."/.signature")->slurp();
     qq{Why I Love Perl - http://shlom.in/joy-of-perl},
 );
 
-my $line = $resource_lines[rand(@resource_lines)];
+my $line = _pick(\@resource_lines);
 
 my $quotes_text = io()->encoding('utf-8')->file("$script_dir/shlomif-sig-quotes")->slurp;
 
 my @quotes = grep { /\S/ } split(/^\n*%\n*$/ms, $quotes_text);
 
-my $quote = $quotes[rand(scalar(@quotes))];
+my $quote = _pick(\@quotes);
 
 $quote =~ s{\n+\z}{}ms;
 $quote =~ s{\A\n+}{}ms;