The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Mock::Data::Plugin::Number - Mock::Data plugin that provides basic numeric generators

SYNOPSIS

  my $mock= Mock::Data->new(['Number']);
  $mock->integer($digits); # signed/unsigned, bit length or digit length
  $mock->decimal([$p,$s]); # [precision,scale] decimal numbers
  $mock->float($digits);   # random significand and exponent floats
  $mock->byte($count);     # string of random bytes
  $mock->sequence($name);  # incrementing named counter, starting from 1
  $mock->uuid;             # UUID version 4 variant 1 (random)

DESCRIPTION

This plugin provides some basic "random number" support.

GENERATORS

integer

  $mock->integer;                    # signed 32-bit by default
  $mock->integer(10);                # up to 10 digits
  $mock->integer({ digits => 10 });
  $mock->integer({ size => 10 });    # alias for digits
  $mock->integer({ bits => 20 });    # up to 20 bits (either signed or unsigned)
  $mock->integer({ unsigned => 1 });

Returns a random integer up to $digits decimal digits (not including sign) or up to $bits (including sign). If $digits and $bits are both specified, $digits wins. If neither are specified, the default is { bits => 32 }. If unsigned is true, this generates non-negative integers.

The randomization chooses the length of the number (either bits or decimal digits) separately from the value of the number. This results in numbers tending toward the middle string length, rather than an even distribution over the range of values. The goal is to look more realistic than if nearly half the values were the maximum length.

decimal

  $str= $mock->decimal($size);
  $str= $mock->decimal([ $size, $scale ]);
  $str= $mock->decimal({ size => [ $size, $scale ] });

$size is the total number of digits (not characters) and $scale is the number of digits to the right of the decimal point.

Note that this generator returns strings, to make sure to avoid floating imprecision.

float

  $str= $mock->float;
  $str= $mock->float({ bits => $significand_bits });
  $str= $mock->float($digits);
  $str= $mock->float({ size => $digits });

If a number of "digits" is requested, this calls "decimal" with a random scale and returns a string.

Else, it operates on bits, choosing a random significand, exponent, and sign. If a number if bits is requested, this applies to the length of the significand. For example, bits => 23 means that you can pack the number as a IEEE754 32-bit float and get back the original number, because the IEEE 754 32-bit has a 23 bit significand. Like the 'digits' mode, this picks an exponent within the significand, to avoid scientific notation.

The default is bits => 23.

byte

  $byte= $mock->byte;          # a single random byte
  $str=  $mock->byte($count);  # returns $count random bytes

sequence

  $int= $mock->sequence($seq_name);
  $int= $mock->sequence({ sequence_name => $seq_name });

Returns the next number in the named sequence, starting with 1. The sequence name is required. The state of the sequence is stored in $mock->generator_state->{"Number::sequence"}{$seq_name}.

uuid

Return a "version 4" UUID composed of weak random bits from rand().

AUTHOR

Michael Conrad <mike@nrdvana.net>

VERSION

version 0.03

COPYRIGHT AND LICENSE

This software is copyright (c) 2021 by Michael Conrad.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.