| 1 | # BioPerl module for Bio::Community::Role::PRNG | ||||||||||||||
| 2 | # | ||||||||||||||
| 3 | # Please direct questions and support issues to <bioperl-l@bioperl.org> | ||||||||||||||
| 4 | # | ||||||||||||||
| 5 | # Copyright 2011-2014 Florent Angly <florent.angly@gmail.com> | ||||||||||||||
| 6 | # | ||||||||||||||
| 7 | # You may distribute this module under the same terms as perl itself | ||||||||||||||
| 8 | |||||||||||||||
| 9 | |||||||||||||||
| 10 | =head1 NAME | ||||||||||||||
| 11 | |||||||||||||||
| 12 | Bio::Community::Role::PRNG - Role for objects that use a pseudo-random number generator | ||||||||||||||
| 13 | |||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||
| 15 | |||||||||||||||
| 16 | package My::Package; | ||||||||||||||
| 17 | |||||||||||||||
| 18 | use Moose; | ||||||||||||||
| 19 | with 'Bio::Community::Role::PRNG'; | ||||||||||||||
| 20 | |||||||||||||||
| 21 | # Use set_seed(), get_seed() and rand() as needed | ||||||||||||||
| 22 | # ... | ||||||||||||||
| 23 | |||||||||||||||
| 24 | 1; | ||||||||||||||
| 25 | |||||||||||||||
| 26 | =head1 DESCRIPTION | ||||||||||||||
| 27 | |||||||||||||||
| 28 | This role provides the capability to generate random numbers using a specified | ||||||||||||||
| 29 | or automatically determined seed. This module uses the superior Mersenne-Twister | ||||||||||||||
| 30 | algorithm provided by the L<Math::Random::MT> module to draw uniformly | ||||||||||||||
| 31 | distributed random numbers. | ||||||||||||||
| 32 | |||||||||||||||
| 33 | =head1 AUTHOR | ||||||||||||||
| 34 | |||||||||||||||
| 35 | Florent Angly L<florent.angly@gmail.com> | ||||||||||||||
| 36 | |||||||||||||||
| 37 | =head1 SUPPORT AND BUGS | ||||||||||||||
| 38 | |||||||||||||||
| 39 | User feedback is an integral part of the evolution of this and other Bioperl | ||||||||||||||
| 40 | modules. Please direct usage questions or support issues to the mailing list, | ||||||||||||||
| 41 | L<bioperl-l@bioperl.org>, rather than to the module maintainer directly. Many | ||||||||||||||
| 42 | experienced and reponsive experts will be able look at the problem and quickly | ||||||||||||||
| 43 | address it. Please include a thorough description of the problem with code and | ||||||||||||||
| 44 | data examples if at all possible. | ||||||||||||||
| 45 | |||||||||||||||
| 46 | If you have found a bug, please report it on the BioPerl bug tracking system | ||||||||||||||
| 47 | to help us keep track the bugs and their resolution: | ||||||||||||||
| 48 | L<https://redmine.open-bio.org/projects/bioperl/> | ||||||||||||||
| 49 | |||||||||||||||
| 50 | =head1 COPYRIGHT | ||||||||||||||
| 51 | |||||||||||||||
| 52 | Copyright 2011-2014 by Florent Angly <florent.angly@gmail.com> | ||||||||||||||
| 53 | |||||||||||||||
| 54 | This library is free software; you can redistribute it and/or modify | ||||||||||||||
| 55 | it under the same terms as Perl itself, either Perl version 5.10.1 or, | ||||||||||||||
| 56 | at your option, any later version of Perl 5 you may have available. | ||||||||||||||
| 57 | |||||||||||||||
| 58 | =head1 APPENDIX | ||||||||||||||
| 59 | |||||||||||||||
| 60 | The rest of the documentation details each of the object | ||||||||||||||
| 61 | methods. Internal methods are usually preceded with a _ | ||||||||||||||
| 62 | |||||||||||||||
| 63 | =cut | ||||||||||||||
| 64 | |||||||||||||||
| 65 | |||||||||||||||
| 66 | package Bio::Community::Role::PRNG; | ||||||||||||||
| 67 | |||||||||||||||
| 68 | 4 | ▶ | use Moose::Role; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 69 | 4 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 70 | 4 | ▶ | use Method::Signatures; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 71 | 4 | ▶ | use Bio::Community::Types; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 72 | 4 | ▶ | use Math::Random::MT; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 73 | |||||||||||||||
| 74 | |||||||||||||||
| 75 | has _prng => ( | ||||||||||||||
| 76 | is => 'rw', | ||||||||||||||
| 77 | #isa => 'Math::Random::MT', | ||||||||||||||
| 78 | required => 0, | ||||||||||||||
| 79 | default => undef, | ||||||||||||||
| 80 | 3 | ▶ | default => sub { Math::Random::MT->new( shift->_seed ) }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 81 | 4 | init_arg => undef, | |||||||||||||
| 82 | lazy => 1, | ||||||||||||||
| 83 | predicate => '_has_prng', | ||||||||||||||
| 84 | ); | ||||||||||||||
| 85 | |||||||||||||||
| 86 | |||||||||||||||
| 87 | # The -seed constructor | ||||||||||||||
| 88 | |||||||||||||||
| 89 | 4 | has _seed => ( | |||||||||||||
| 90 | is => 'rw', | ||||||||||||||
| 91 | isa => 'Maybe[PositiveInt]', | ||||||||||||||
| 92 | required => 0, | ||||||||||||||
| 93 | default => undef, | ||||||||||||||
| 94 | init_arg => '-seed', | ||||||||||||||
| 95 | lazy => 1, | ||||||||||||||
| 96 | ); | ||||||||||||||
| 97 | |||||||||||||||
| 98 | |||||||||||||||
| 99 | =head2 get_seed, set_seed | ||||||||||||||
| 100 | |||||||||||||||
| 101 | Usage : $prng->set_seed(1234513451); | ||||||||||||||
| 102 | Function: Get or set the seed used to generate the random numbers. | ||||||||||||||
| 103 | Args : Positive integer | ||||||||||||||
| 104 | Returns : Positive integer | ||||||||||||||
| 105 | |||||||||||||||
| 106 | =cut | ||||||||||||||
| 107 | |||||||||||||||
| 108 | 4 | ▶ | method get_seed { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
get_seed: called CC=2 SCAR=7.0
Branch: 1 branch
| |||||||||||||||
| 109 | 1 | $self->_prng->get_seed; | |||||||||||||
| 110 | } | ||||||||||||||
| 111 | |||||||||||||||
| 112 | |||||||||||||||
| 113 | 4 | ▶ | method set_seed ($int) { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.1
Subroutine
set_seed: called CC=3 SCAR=11.2
Branch: 2 branches
| |||||||||||||||
| 114 | 1 | $self->_prng->set_seed($int); | |||||||||||||
| 115 | } | ||||||||||||||
| 116 | |||||||||||||||
| 117 | |||||||||||||||
| 118 | =head2 rand | ||||||||||||||
| 119 | |||||||||||||||
| 120 | Usage : my $num = $prng->rand($max); | ||||||||||||||
| 121 | Function: Generate a number uniformly distributed in [0, $max) | ||||||||||||||
| 122 | Args : Number | ||||||||||||||
| 123 | Returns : Number | ||||||||||||||
| 124 | |||||||||||||||
| 125 | =cut | ||||||||||||||
| 126 | |||||||||||||||
| 127 | 4 | ▶ | method rand ($num?) { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
rand: called CC=2 SCAR=7.0
Branch: 1 branch
| |||||||||||||||
| 128 | 31 | $self->_prng->rand($num); | |||||||||||||
| 129 | } | ||||||||||||||
| 130 | |||||||||||||||
| 131 | |||||||||||||||
| 132 | |||||||||||||||
| 133 | 4 | 1; | |||||||||||||