#!/usr/bin/perl

package grand;

#######################################
#                                     #
#     Randomizer                      #
#                                     #
#    (C) 2017 Domero                  #
#                                     #
#######################################

use strict;
use warnings;
use Exporter;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);

$VERSION     = '1.02';
@ISA         = qw(Exporter);
@EXPORT      = ();
@EXPORT_OK   = qw(rand randomhex);

1;

sub creatematrix {
  my @keys=();
  for (my $i=0;$i<16;$i++) {
    for (my $j=0;$j<16;$j++) {
      push @keys,$j
    }
  }
  my $enc=[]; my $num=256;
  for (my $i=0;$i<256;$i++) {
    my $rnd=int rand($num);
    push @$enc,$keys[$rnd];
    splice(@keys,$rnd,1);
    $num--
  }
  return $enc
}

sub getnibble {
  my $matrix=creatematrix();
  my $rnd=int rand(256);
  return $matrix->[$rnd]
}

sub rand {
  my ($int) = @_;
  my $len=0; my $num="0.";
  while ($len<15) {
    my $rnd=getnibble();
    if ($rnd<10) {
      $num.=chr(48+$rnd); $len++
    }
  }
  if ($int) {
    return int $num*$int
  }
  return $num
}

sub randomhex {
  my ($len) = @_;
  if ($len<0) { return }
  my $out="";
  while ($len>0) {
    my $code=getnibble();
    if ($code < 10) {
      $out.=chr(48 + $code)
    } else {
      $out.=chr(55 + $code)
    }
    $len--
  }
  return $out
}

# EOF grand.pm (C) 2017 Domero