Venus::Random
Random Class
Random Class for Perl 5
method: bit method: boolean method: byte method: character method: collect method: digit method: float method: letter method: lowercased method: nonzero method: number method: pick method: range method: repeat method: reseed method: reset method: restore method: select method: symbol method: uppercased
package main;
use Venus::Random;
my $random = Venus::Random->new(42);
# my $bit = $random->bit;
# 1
This package provides an object-oriented interface for Perl's pseudo-random number generator (or PRNG) which produces a deterministic sequence of bits which approximate true randomness.
Venus::Kind::Utility
Venus::Role::Accessible Venus::Role::Buildable Venus::Role::Valuable
The bit method returns a 1 or 0 value, randomly.
bit() (Int)
{ since => '1.11', }
=example-1 bit
# given: synopsis
package main;
my $bit = $random->bit;
# 0
# $bit = $random->bit;
# 1
The boolean method returns a true or false value, randomly.
boolean() (Bool)
{ since => '1.11', }
=example-1 boolean
# given: synopsis
package main;
my $boolean = $random->boolean;
# 0
# $boolean = $random->boolean;
# 1
The byte method returns random byte characters, randomly.
byte() (Str)
{ since => '1.11', }
=example-1 byte
# given: synopsis
package main;
my $byte = $random->byte;
# "\xBE"
# $byte = $random->byte;
# "W"
The character method returns a random character, which is either a "digit", "letter", or "symbol" value.
character() (Str)
{ since => '1.11', }
=example-1 character
# given: synopsis
package main;
my $character = $random->character;
# ")"
# $character = $random->character;
# 4
The collect method dispatches to the specified method or coderef, repeatedly based on the number of $times specified, and returns the random concatenated results from each dispatched call. By default, if no arguments are provided, this method dispatches to "digit".
collect(Int $times, Str|CodeRef $code, Any @args) (Int|Str)
{ since => '1.11', }
=example-1 collect
# given: synopsis
package main;
my $collect = $random->collect;
# 7
# $collect = $random->collect;
# 3
The digit method returns a random digit between 0 and 9.
digit() (Int)
{ since => '1.11', }
=example-1 digit
# given: synopsis
package main;
my $digit = $random->digit;
# 7
# $digit = $random->digit;
# 3
The float method returns a random float.
float(Int $place, Int $from, Int $upto) (Num)
{ since => '1.11', }
=example-1 float
# given: synopsis
package main;
my $float = $random->float;
# 1447361.5
# $float = $random->float;
# "0.0000"
The letter method returns a random letter, which is either an "uppercased" or "lowercased" value.
letter() (Str)
{ since => '1.11', }
=example-1 letter
# given: synopsis
package main;
my $letter = $random->letter;
# "i"
# $letter = $random->letter;
# "K"
The lowercased method returns a random lowercased letter.
lowercased() (Str)
{ since => '1.11', }
=example-1 lowercased
# given: synopsis
package main;
my $lowercased = $random->lowercased;
# "t"
# $lowercased = $random->lowercased;
# "i"
The nonzero method dispatches to the specified method or coderef and returns the random value ensuring that it's never zero, not even a percentage of zero. By default, if no arguments are provided, this method dispatches to "digit".
nonzero(Str|CodeRef $code, Any @args) (Int|Str)
{ since => '1.11', }
=example-1 nonzero
# given: synopsis
package main;
my $nonzero = $random->nonzero;
# 7
# $nonzero = $random->nonzero;
# 3
The number method returns a random number within the range provided. If no arguments are provided, the range is from 0 to 2147483647. If only the first argument is provided, it's treated as the desired length of the number.
number(Int $from, Int $upto) (Num)
{ since => '1.11', }
=example-1 number
# given: synopsis
package main;
my $number = $random->number;
# 3427014
# $number = $random->number;
# 3
The pick method is the random number generator and returns a random number. By default, calling this method is equivalent to call "rand" in perlfunc. This method can be overridden in a subclass to provide a custom generator, e.g. a more cyptographically secure generator.
pick(Num $data) (Num)
{ since => '1.23', }
=example-1 pick
# given: synopsis
package main;
my $pick = $random->pick;
# 0.744525000061007
# $pick = $random->pick;
# 0.342701478718908
The range method returns a random number within the range provided. If no arguments are provided, the range is from 0 to 2147483647.
range(Str $name) (ArrayRef)
{ since => '1.11', }
=example-1 range
# given: synopsis
package main;
my $range = $random->range(1, 10);
# 8
# $range = $random->range(1, 10);
# 4
The repeat method dispatches to the specified method or coderef, repeatedly based on the number of $times specified, and returns the random results from each dispatched call. In list context, the results from each call is returned as a list, in scalar context the results are concatenated.
repeat(Int $times, Str|CodeRef $code, Any @args) (Int|Str)
{ since => '1.11', }
=example-1 repeat
# given: synopsis
package main;
my @repeat = $random->repeat(2);
# (7, 3)
# @repeat = $random->repeat(2);
# (1, 4)
The reseed method sets the "srand" in perlfunc (i.e. the PRNG seed) to the value provided, or the default value used on instanstiation when no seed is passed to the constructor. This method returns the object that invoked it.
reseed(Str $seed) (Random)
{ since => '1.11', }
=example-1 reseed
# given: synopsis
package main;
my $reseed = $random->reseed;
# bless({value => ...}, "Venus::Random")
# my $bit = $random->bit;
# 0
The reset method sets the "srand" in perlfunc (i.e. the PRNG seed) to the default value used on instanstiation when no seed is passed to the constructor. This method returns the object that invoked it.
reset() (Random)
{ since => '1.11', }
=example-1 reset
# given: synopsis
package main;
my $reset = $random->reset;
# bless({value => ...}, "Venus::Random")
The restore method sets the "srand" in perlfunc (i.e. the PRNG seed) to the original value used by "rand" in perlfunc. This method returns the object that invoked it.
restore() (Random)
{ since => '1.11', }
=example-1 restore
# given: synopsis
package main;
my $restore = $random->restore;
# bless({value => ...}, "Venus::Random")
The select method returns a random value from the "hashref" or "arrayref" provided.
select(ArrayRef|HashRef $data) (Any)
{ since => '1.11', }
=example-1 select
# given: synopsis
package main;
my $select = $random->select(["a".."d"]);
# "c"
# $select = $random->select(["a".."d"]);
# "b"
The symbol method returns a random symbol.
symbol() (Str)
{ since => '1.11', }
=example-1 symbol
# given: synopsis
package main;
my $symbol = $random->symbol;
# "'"
# $symbol = $random->symbol;
# ")"
The uppercased method returns a random uppercased letter.
uppercased() (Str)
{ since => '1.11', }
=example-1 uppercased
# given: synopsis
package main;
my $uppercased = $random->uppercased;
# "T"
# $uppercased = $random->uppercased;
# "I"
t/Venus.t: pdml: authors t/Venus.t: pdml: license
90 POD Errors
The following errors were encountered while parsing the POD:
- Around line 22:
Unknown directive: =name
- Around line 30:
Unknown directive: =tagline
- Around line 38:
Unknown directive: =abstract
- Around line 46:
Unknown directive: =includes
- Around line 73:
Unknown directive: =synopsis
- Around line 95:
Unknown directive: =description
- Around line 105:
Unknown directive: =inherits
- Around line 113:
Unknown directive: =integrates
- Around line 123:
Unknown directive: =method
- Around line 127:
Unknown directive: =signature
- Around line 131:
Unknown directive: =metadata
- Around line 213:
Unknown directive: =method
- Around line 217:
Unknown directive: =signature
- Around line 221:
Unknown directive: =metadata
- Around line 303:
Unknown directive: =method
- Around line 307:
Unknown directive: =signature
- Around line 311:
Unknown directive: =metadata
- Around line 393:
Unknown directive: =method
- Around line 398:
Unknown directive: =signature
- Around line 402:
Unknown directive: =metadata
- Around line 484:
Unknown directive: =method
- Around line 491:
Unknown directive: =signature
- Around line 495:
Unknown directive: =metadata
- Around line 591:
=cut found outside a pod block. Skipping to next block.
- Around line 668:
=cut found outside a pod block. Skipping to next block.
- Around line 744:
=cut found outside a pod block. Skipping to next block.
- Around line 806:
Unknown directive: =method
- Around line 810:
Unknown directive: =signature
- Around line 814:
Unknown directive: =metadata
- Around line 896:
Unknown directive: =method
- Around line 900:
Unknown directive: =signature
- Around line 904:
Unknown directive: =metadata
- Around line 1000:
=cut found outside a pod block. Skipping to next block.
- Around line 1076:
=cut found outside a pod block. Skipping to next block.
- Around line 1152:
=cut found outside a pod block. Skipping to next block.
- Around line 1214:
Unknown directive: =method
- Around line 1219:
Unknown directive: =signature
- Around line 1223:
Unknown directive: =metadata
- Around line 1305:
Unknown directive: =method
- Around line 1309:
Unknown directive: =signature
- Around line 1313:
Unknown directive: =metadata
- Around line 1395:
Unknown directive: =method
- Around line 1401:
Unknown directive: =signature
- Around line 1405:
Unknown directive: =metadata
- Around line 1501:
=cut found outside a pod block. Skipping to next block.
- Around line 1577:
=cut found outside a pod block. Skipping to next block.
- Around line 1653:
=cut found outside a pod block. Skipping to next block.
- Around line 1715:
Unknown directive: =method
- Around line 1721:
Unknown directive: =signature
- Around line 1725:
Unknown directive: =metadata
- Around line 1821:
=cut found outside a pod block. Skipping to next block.
- Around line 1897:
=cut found outside a pod block. Skipping to next block.
- Around line 1973:
=cut found outside a pod block. Skipping to next block.
- Around line 2035:
Unknown directive: =method
- Around line 2042:
Unknown directive: =signature
- Around line 2046:
Unknown directive: =metadata
- Around line 2142:
=cut found outside a pod block. Skipping to next block.
- Around line 2218:
=cut found outside a pod block. Skipping to next block.
- Around line 2280:
Unknown directive: =method
- Around line 2285:
Unknown directive: =signature
- Around line 2289:
Unknown directive: =metadata
- Around line 2385:
=cut found outside a pod block. Skipping to next block.
- Around line 2461:
=cut found outside a pod block. Skipping to next block.
- Around line 2537:
=cut found outside a pod block. Skipping to next block.
- Around line 2599:
Unknown directive: =method
- Around line 2606:
Unknown directive: =signature
- Around line 2610:
Unknown directive: =metadata
- Around line 2683:
=cut found outside a pod block. Skipping to next block.
- Around line 2809:
=cut found outside a pod block. Skipping to next block.
- Around line 2871:
Unknown directive: =method
- Around line 2877:
Unknown directive: =signature
- Around line 2881:
Unknown directive: =metadata
- Around line 2926:
=cut found outside a pod block. Skipping to next block.
- Around line 2938:
Unknown directive: =method
- Around line 2944:
Unknown directive: =signature
- Around line 2948:
Unknown directive: =metadata
- Around line 2975:
Unknown directive: =method
- Around line 2981:
Unknown directive: =signature
- Around line 2985:
Unknown directive: =metadata
- Around line 3012:
Unknown directive: =method
- Around line 3017:
Unknown directive: =signature
- Around line 3021:
Unknown directive: =metadata
- Around line 3117:
=cut found outside a pod block. Skipping to next block.
- Around line 3179:
Unknown directive: =method
- Around line 3183:
Unknown directive: =signature
- Around line 3187:
Unknown directive: =metadata
- Around line 3269:
Unknown directive: =method
- Around line 3273:
Unknown directive: =signature
- Around line 3277:
Unknown directive: =metadata
- Around line 3359:
Unknown directive: =partials