1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
use 5.006;
use strict;
use warnings;
package Math::Random::OO::Uniform;
# ABSTRACT: Generates random numbers from the uniform distribution
our $VERSION = '0.22'; # VERSION
# Required modules
use Carp;
use Params::Validate ':all';
# ISA
use base qw( Class::Accessor::Fast );
{
my $param_spec = {
low => { type => SCALAR },
high => { type => SCALAR }
};
__PACKAGE__->mk_accessors( keys %$param_spec );
#__PACKAGE__->mk_ro_accessors( keys %$param_spec );
sub new {
my $class = shift;
my $self = bless {}, ref($class) ? ref($class) : $class;
if ( @_ > 1 ) {
my ( $low, $high ) = sort { $a <=> $b } @_[ 0, 1 ]; # DWIM
$self->low($low);
$self->high($high);
}
elsif ( @_ == 1 ) {
$self->low(0);
$self->high( $_[0] );
}
else {
$self->low(0);
$self->high(0);
}
return $self;
}
}
sub seed {
my $self = shift;
srand( $_[0] );
}
sub next {
my ($self) = @_;
my $rnd = rand( $self->high - $self->low ) + $self->low;
return $rnd;
}
1;
__END__
=pod
=encoding utf-8
=head1 NAME
Math::Random::OO::Uniform - Generates random numbers from the uniform distribution
=head1 VERSION
version 0.22
=head1 SYNOPSIS
use Math::Random::OO::Uniform;
push @prngs,
Math::Random::OO::Uniform->new(), # range [0,1)
Math::Random::OO::Uniform->new(5), # range [0,5)
Math::Random::OO::Uniform->new(-1,1); # range [-1,1)
$_->seed(42) for @prngs;
print( $_->next() . "\n" ) for @prngs;
=head1 DESCRIPTION
This subclass of L<Math::Random::OO> generates random reals from a uniform
probability distribution.
=head1 METHODS
=head2 C<new>
$prng1 = Math::Random::OO::Uniform->new();
$prng2 = Math::Random::OO::Uniform->new($high);
$prng3 = Math::Random::OO::Uniform->new($low,$high);
C<new> takes up to two optional parameters and returns a new
C<Math::Random::OO::Uniform> object. With no parameters, the object generates
random numbers in the range of zero (inclusive) to one (exclusive). With a
single parameter, the object generates random numbers from zero (inclusive) to
the value of the parameter (exclusive). Note, the object does this with
multiplication, so if the parameter is negative, the function will return
negative numbers. This is a feature or bug, depending on your point of view.
With two parameters, the object generates random numbers from the first
parameter (inclusive) to the second parameter (exclusive). (Actually, as
long as you have two parameters, C<new> will put them in the right order).
=head2 C<seed>
$rv = $prng->seed( @seeds );
This method seeds the random number generator. At the moment, only the
first seed value matters. It should be a positive integer.
=head2 C<next>
$rnd = $prng->next();
This method returns the next random number from the random number generator.
It does not take any parameters.
=head1 AUTHOR
David Golden <dagolden@cpan.org>
=head1 COPYRIGHT AND LICENSE
This software is Copyright (c) 2013 by David Golden.
This is free software, licensed under:
The Apache License, Version 2.0, January 2004
=cut
|