blib/lib/Venus/Random.pm

filter: Statementstmt 94.2% 99 / 105 Branchbran 72.2% 26 / 36 Conditioncond 56.2% 27 / 48 MC/DCmcdc 31.0% 9 / 29 Subroutinesub 92.5% 25 / 27 Podpod 95.4% 21 / 22 totaltotal 77.5% 207 / 267 SCAR 38.6
CC
37
Cov
80%
range
12.0
float
10.1
nonzero
7.2
CRAP 47.3
« blib/lib/Venus/Os.pm ↑ Summary blib/lib/Venus/Error.pm »
1 package Venus::Random;
2  
3 1 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Venus::Class 'base', 'with';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 base 'Venus::Kind::Utility';
11  
12 1 with 'Venus::Role::Valuable';
13 1 with 'Venus::Role::Buildable';
14 1 with 'Venus::Role::Accessible';
15  
16 # STATE
17  
18 1 state $ORIG_SEED = srand;
19 1 state $SELF_SEED = substr(((time ^ $$) ** 2), 0, length($ORIG_SEED));
20 1 srand $ORIG_SEED;
21  
22 # BUILDERS
23  
24 sub build_self {
25 80 ▶ my ($self, $data) = @_;
Subroutine build_self: called CC=1 SCAR=0.0
Pod undocumented
26  
27 80 $self->reseed($self->value);
28  
29 80 return $self;
30 }
31  
32 # METHODS
33  
34 sub assertion {
35 0 ▶ my ($self) = @_;
Subroutine assertion: not called CC=1 SCAR=6.9
Pod documented
36  
37 0 my $assertion = $self->SUPER::assertion;
38  
39 $assertion->match('number')->format(sub{
40 0 ▶ (ref $self || $self)->new($_)
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
41 0 });
42  
43 0 return $assertion;
44 }
45  
46 sub bit {
47 52 ▶ my ($self) = @_;
Subroutine bit: called CC=1 SCAR=0.0
Pod documented
48  
49 52 return $self->select([1, 0]);
50 }
51  
52 sub boolean {
53 51 ▶ my ($self) = @_;
Subroutine boolean: called CC=1 SCAR=0.0
Pod documented
54  
55 51 return $self->select([true, false]);
56 }
57  
58 sub byte {
59 51 ▶ my ($self) = @_;
Subroutine byte: called CC=1 SCAR=0.0
Pod documented
60  
61 51 return chr(int($self->pick * 256));
62 }
63  
64 sub character {
65 663 ▶ my ($self) = @_;
Subroutine character: called CC=1 SCAR=0.0
Pod documented
66  
67 663 my $code = $self->select(['digit', 'letter', 'symbol']);
68  
69 663 return $self->$code;
70 }
71  
72 sub collect {
73 204 ▶ my ($self, $times, $code, @args) = @_;
Subroutine collect: called CC=1 SCAR=0.0
Pod documented
74  
75 204 return scalar($self->repeat($times, $code, @args));
76 }
77  
78 sub digit {
79 1016 ▶ my ($self) = @_;
Subroutine digit: called CC=1 SCAR=0.0
Pod documented
80  
81 1016 return int($self->pick(10));
82 }
83  
84 sub float {
85 306 ▶ my ($self, $place, $from, $upto) = @_;
Subroutine float: called CC=7 SCAR=23.2
Pod documented
86  
87 306 ▶ $from //= 0;
Condition100%2/2
$from //= 0
l!l
102204
MC/DC100%1/1
$from //= 0
$from
Truth table100%2/2
A
A= $from
Aresult
11
00
88 306 ▶ $upto //= $self->number;
Condition66%2/3
$upto //= $self->number
l!l&&r!l&&!r
1022040
MC/DC0%0/2
$upto //= $self->number
$upto $self->number
Truth table66%2/3
A //= B
A= $uptoB= $self->number
ABresult
1X1
011
000
89  
90 306 ▶ my $tmp; $tmp = $from and $from = $upto and $upto = $tmp if $from > $upto;
Branch: 1 branch
BranchTrueFalseTotal
if $from > $upto 0 306 306
Condition12%1/8
$from > $upto && (($tmp = $from) && ($from = $upto) && ($upto = $tmp))
!ll
3060
$tmp = $from and $from = $upto and $upto = $tmp
!ll&&!rl&&r
000
$tmp = $from and $from = $upto
!ll&&!rl&&r
000
MC/DC0%0/4
$from > $upto && (($tmp = $from) && ($from = $upto) && ($upto = $tmp))
$from > $upto $tmp = $from $from = $upto $upto = $tmp
Truth table0%0/5
A && B and C and D
A= $from > $uptoB= $tmp = $fromC= $from = $uptoD= $upto = $tmp
ABCDresult
0XXX0
10XX0
110X0
11100
11111
91  
92 306 ▶ $place //= $self->nonzero;
Condition66%2/3
$place //= $self->nonzero
l!l&&r!l&&!r
1531530
MC/DC0%0/2
$place //= $self->nonzero
$place $self->nonzero
Truth table66%2/3
A //= B
A= $placeB= $self->nonzero
ABresult
1X1
011
000
93  
94 306 return sprintf("%.${place}f", $from + rand() * ($upto - $from));
95 }
96  
97 sub letter {
98 540 ▶ my ($self) = @_;
Subroutine letter: called CC=1 SCAR=0.0
Pod documented
99  
100 540 my $code = $self->select(['uppercased', 'lowercased']);
101  
102 540 return $self->$code;
103 }
104  
105 sub lowercased {
106 317 ▶ my ($self) = @_;
Subroutine lowercased: called CC=1 SCAR=0.0
Pod documented
107  
108 317 return lc(chr($self->range(97, 122)));
109 }
110  
111 sub pick {
112 4294 ▶ my ($self, $data) = @_;
Subroutine pick: called CC=2 SCAR=6.9
Pod documented
113  
114 4294 ▶ return $data ? rand($data) : rand;
Branch: 1 branch
BranchTrueFalseTotal
$data ? : 4113 181 4294
115 }
116  
117 sub nonzero {
118 385 ▶ my ($self, $code, @args) = @_;
Subroutine nonzero: called CC=7 SCAR=19.8
Pod documented
119  
120 385 ▶ $code ||= 'digit';
Condition100%2/2
$code ||= "digit"
l!l
153232
MC/DC100%1/1
$code ||= "digit"
$code
Truth table100%2/2
A
A= $code
Aresult
11
00
121  
122 385 my $value = $self->$code(@args);
123  
124 return
125 385 ▶ ($value < 0 && $value > -1) ? ($value + -1)
Branch: 3 branches
BranchTrueFalseTotal
$value < 0 && $value > -1 ? : 0 385 385
$value < 1 && $value > 0 ? : 51 334 385
$value == 0 ? : 28 306 334
Condition66%4/6
$value < 0 and $value > -1
!ll&&!rl&&r
38500
$value < 1 and $value > 0
!ll&&!rl&&r
3062851
MC/DC0%0/2
$value < 0 and $value > -1
$value < 0 $value > -1
MC/DC100%2/2
$value < 1 and $value > 0
$value < 1 $value > 0
Truth table33%1/3
A and B
A= $value < 0B= $value > -1
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $value < 1B= $value > 0
ABresult
0X0
100
111
126 : (($value < 1 && $value > 0) ? ($value + 1)
127 : ($value == 0 ? $self->nonzero : $value));
128 }
129  
130 sub number {
131 510 ▶ my ($self, $from, $upto) = @_;
Subroutine number: called CC=5 SCAR=16.2
Pod documented
132  
133 510 ▶ $upto //= 0;
Condition100%2/2
$upto //= 0
l!l
153357
MC/DC100%1/1
$upto //= 0
$upto
Truth table100%2/2
A
A= $upto
Aresult
11
00
134 510 ▶ $from //= $self->digit;
Condition66%2/3
$from //= $self->digit
l!l&&r!l&&!r
2043060
MC/DC0%0/2
$from //= $self->digit
$from $self->digit
Truth table66%2/3
A //= B
A= $fromB= $self->digit
ABresult
1X1
011
000
135  
136 510 ▶ return $self->range($from, $upto) if $upto;
Branch: 1 branch
BranchTrueFalseTotal
if $upto 153 357 510
137  
138 357 ▶ return int($self->pick(10 ** ($from > 9 ? 9 : $from) -1));
Branch: 1 branch
BranchTrueFalseTotal
$from > 9 ? : 0 357 357
139 }
140  
141 sub range {
142 2666 ▶ my ($self, $from, $upto) = @_;
Subroutine range: called CC=9 SCAR=24.8
Pod documented
143  
144 2666 ▶ return 0 if !defined $from;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from 0 2666 2666
145 2666 ▶ return 0 if !defined $upto && $from == 0;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $upto and $from == 0 0 2666 2666
Condition33%1/3
not defined $upto and $from == 0
!ll&&!rl&&r
266600
MC/DC0%0/2
not defined $upto and $from == 0
not defined $upto $from == 0
Truth table33%1/3
not(A) and B
A= not defined $uptoB= $from == 0
ABresult
0X0
100
111
146  
147 2666 ▶ return $from if $from == $upto;
Branch: 1 branch
BranchTrueFalseTotal
if $from == $upto 0 2666 2666
148  
149 2666 my $ceil = 2147483647;
150  
151 2666 ▶ $from = 0 if !$from || $from > $ceil;
Branch: 1 branch
BranchTrueFalseTotal
if not $from or $from > $ceil 1768 898 2666
Condition66%2/3
not $from or $from > $ceil
l!l&&r!l&&!r
17680898
MC/DC50%1/2
not $from or $from > $ceil
not $from $from > $ceil
Truth table66%2/3
not(A) or B
A= not $fromB= $from > $ceil
ABresult
1X1
011
000
152 2666 ▶ $upto = $ceil if !$upto || $upto > $ceil;
Branch: 1 branch
BranchTrueFalseTotal
if not $upto or $upto > $ceil 0 2666 2666
Condition33%1/3
not $upto or $upto > $ceil
l!l&&r!l&&!r
002666
MC/DC0%0/2
not $upto or $upto > $ceil
not $upto $upto > $ceil
Truth table33%1/3
not(A) or B
A= not $uptoB= $upto > $ceil
ABresult
1X1
011
000
153  
154 2666 return $from + int($self->pick(($upto-$from) + 1));
155 }
156  
157 sub repeat {
158 332 ▶ my ($self, $times, $code, @args) = @_;
Subroutine repeat: called CC=5 SCAR=16.1
Pod documented
159  
160 332 my @values;
161  
162 332 ▶ $code ||= 'digit';
Condition100%2/2
$code ||= "digit"
l!l
204128
MC/DC100%1/1
$code ||= "digit"
$code
Truth table100%2/2
A
A= $code
Aresult
11
00
163 332 ▶ $times ||= 1;
Condition100%2/2
$times ||= 1
l!l
28151
MC/DC100%1/1
$times ||= 1
$times
Truth table100%2/2
A
A= $times
Aresult
11
00
164  
165 332 push @values, $self->$code(@args) for 1..$times;
166  
167 332 ▶ return wantarray ? (@values) : join('', @values);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 128 204 332
168 }
169  
170 sub reseed {
171 84 ▶ my ($self, $seed) = @_;
Subroutine reseed: called CC=3 SCAR=11.0
Pod documented
172  
173 84 ▶ my $THIS_SEED = !$seed || $seed =~ /\D/ ? $SELF_SEED : $seed;
Branch: 1 branch
BranchTrueFalseTotal
!$seed || $seed =~ /\D/u ? : 1 83 84
Condition66%2/3
not $seed or $seed =~ /\D/u
l!l&&r!l&&!r
1083
MC/DC50%1/2
not $seed or $seed =~ /\D/u
not $seed $seed =~ /\D/u
Truth table66%2/3
not(A) or B
A= not $seedB= $seed =~ /\D/u
ABresult
1X1
011
000
174  
175 84 $self->value($THIS_SEED);
176  
177 84 srand $THIS_SEED;
178  
179 84 return $self;
180 }
181  
182 sub reset {
183 1 ▶ my ($self) = @_;
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
184  
185 1 $self->reseed($SELF_SEED);
186  
187 1 srand $SELF_SEED;
188  
189 1 return $self;
190 }
191  
192 sub restore {
193 1 ▶ my ($self) = @_;
Subroutine restore: called CC=1 SCAR=0.0
Pod documented
194  
195 1 $self->reseed($ORIG_SEED);
196  
197 1 srand $ORIG_SEED;
198  
199 1 return $self;
200 }
201  
202 sub select {
203 1666 ▶ my ($self, $data) = @_;
Subroutine select: called CC=5 SCAR=16.6
Pod documented
204  
205 1666 ▶ if (UNIVERSAL::isa($data, 'ARRAY')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($data, "ARRAY")) 1615 51 1666
206 1615 my $keys = @$data;
207 1615 ▶ my $rand = $self->range(0, $keys <= 0 ? 0 : $keys - 1);
Branch: 1 branch
BranchTrueFalseTotal
$keys <= 0 ? : 0 1615 1615
208 1615 return (@$data)[$rand];
209 }
210  
211 51 ▶ if (UNIVERSAL::isa($data, 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($data, "HASH")) 51 0 51
212 51 my $keys = keys(%$data);
213 51 ▶ my $rand = $self->range(0, $keys <= 0 ? 0 : $keys - 1);
Branch: 1 branch
BranchTrueFalseTotal
$keys <= 0 ? : 0 51 51
214 51 return $$data{(sort keys %$data)[$rand]};
215 }
216  
217 0 return undef;
218 }
219  
220 sub symbol {
221 258 ▶ my ($self) = @_;
Subroutine symbol: called CC=1 SCAR=0.0
Pod documented
222  
223 258 state $symbols = [split //, q(~!@#$%^&*\(\)-_=+[]{}\|;:'",./<>?)];
224  
225 258 return $self->select($symbols);
226 }
227  
228 sub uppercased {
229 325 ▶ my ($self) = @_;
Subroutine uppercased: called CC=1 SCAR=0.0
Pod documented
230  
231 325 return uc(chr($self->range(97, 122)));
232 }
233  
234 1 1;
235  
236  
237  
238 =head1 NAME
239  
240 Venus::Random - Random Class
241  
242 =cut
243  
244 =head1 ABSTRACT
245  
246 Random Class for Perl 5
247  
248 =cut
249  
250 =head1 SYNOPSIS
251  
252 package main;
253  
254 use Venus::Random;
255  
256 my $random = Venus::Random->new(42);
257  
258 # my $bit = $random->bit;
259  
260 # 1
261  
262 =cut
263  
264 =head1 DESCRIPTION
265  
266 This package provides an object-oriented interface for Perl's pseudo-random
267 number generator (or PRNG) which produces a deterministic sequence of bits
268 which approximate true randomness.
269  
270 =cut
271  
272 =head1 INHERITS
273  
274 This package inherits behaviors from:
275  
276 L<Venus::Kind::Utility>
277  
278 =cut
279  
280 =head1 INTEGRATES
281  
282 This package integrates behaviors from:
283  
284 L<Venus::Role::Accessible>
285  
286 L<Venus::Role::Buildable>
287  
288 L<Venus::Role::Valuable>
289  
290 =cut
291  
292 =head1 METHODS
293  
294 This package provides the following methods:
295  
296 =cut
297  
298 =head2 bit
299  
300 bit() (number)
301  
302 The bit method returns a C<1> or C<0> value, randomly.
303  
304 I<Since C<1.11>>
305  
306 =over 4
307  
308 =item bit example 1
309  
310 # given: synopsis
311  
312 package main;
313  
314 my $bit = $random->bit;
315  
316 # 0
317  
318 # $bit = $random->bit;
319  
320 # 1
321  
322 =back
323  
324 =cut
325  
326 =head2 boolean
327  
328 boolean() (boolean)
329  
330 The boolean method returns a C<true> or C<false> value, randomly.
331  
332 I<Since C<1.11>>
333  
334 =over 4
335  
336 =item boolean example 1
337  
338 # given: synopsis
339  
340 package main;
341  
342 my $boolean = $random->boolean;
343  
344 # 0
345  
346 # $boolean = $random->boolean;
347  
348 # 1
349  
350 =back
351  
352 =cut
353  
354 =head2 byte
355  
356 byte() (string)
357  
358 The byte method returns random byte characters, randomly.
359  
360 I<Since C<1.11>>
361  
362 =over 4
363  
364 =item byte example 1
365  
366 # given: synopsis
367  
368 package main;
369  
370 my $byte = $random->byte;
371  
372 # "\xBE"
373  
374 # $byte = $random->byte;
375  
376 # "W"
377  
378 =back
379  
380 =cut
381  
382 =head2 character
383  
384 character() (string)
385  
386 The character method returns a random character, which is either a L</digit>,
387 L</letter>, or L</symbol> value.
388  
389 I<Since C<1.11>>
390  
391 =over 4
392  
393 =item character example 1
394  
395 # given: synopsis
396  
397 package main;
398  
399 my $character = $random->character;
400  
401 # ")"
402  
403 # $character = $random->character;
404  
405 # 4
406  
407 =back
408  
409 =cut
410  
411 =head2 collect
412  
413 collect(number $times, string | coderef $code, any @args) (number | string)
414  
415 The collect method dispatches to the specified method or coderef, repeatedly
416 based on the number of C<$times> specified, and returns the random concatenated
417 results from each dispatched call. By default, if no arguments are provided,
418 this method dispatches to L</digit>.
419  
420 I<Since C<1.11>>
421  
422 =over 4
423  
424 =item collect example 1
425  
426 # given: synopsis
427  
428 package main;
429  
430 my $collect = $random->collect;
431  
432 # 7
433  
434 # $collect = $random->collect;
435  
436 # 3
437  
438 =back
439  
440 =over 4
441  
442 =item collect example 2
443  
444 # given: synopsis
445  
446 package main;
447  
448 my $collect = $random->collect(2);
449  
450 # 73
451  
452 # $collect = $random->collect(2);
453  
454 # 14
455  
456 =back
457  
458 =over 4
459  
460 =item collect example 3
461  
462 # given: synopsis
463  
464 package main;
465  
466 my $collect = $random->collect(5, "letter");
467  
468 # "iKWMv"
469  
470 # $collect = $random->collect(5, "letter");
471  
472 # "Papmm"
473  
474  
475  
476  
477 =back
478  
479 =over 4
480  
481 =item collect example 4
482  
483 # given: synopsis
484  
485 package main;
486  
487 my $collect = $random->collect(10, "character");
488  
489 # ")48R+a}[Lb"
490  
491 # $collect = $random->collect(10, "character");
492  
493 # "?&0725^,0w"
494  
495 =back
496  
497 =cut
498  
499 =head2 digit
500  
501 digit() (number)
502  
503 The digit method returns a random digit between C<0> and C<9>.
504  
505 I<Since C<1.11>>
506  
507 =over 4
508  
509 =item digit example 1
510  
511 # given: synopsis
512  
513 package main;
514  
515 my $digit = $random->digit;
516  
517 # 7
518  
519 # $digit = $random->digit;
520  
521 # 3
522  
523 =back
524  
525 =cut
526  
527 =head2 float
528  
529 float(number $place, number $from, number $upto) (number)
530  
531 The float method returns a random float.
532  
533 I<Since C<1.11>>
534  
535 =over 4
536  
537 =item float example 1
538  
539 # given: synopsis
540  
541 package main;
542  
543 my $float = $random->float;
544  
545 # 1447361.5
546  
547 # $float = $random->float;
548  
549 # "0.0000"
550  
551 =back
552  
553 =over 4
554  
555 =item float example 2
556  
557 # given: synopsis
558  
559 package main;
560  
561 my $float = $random->float(2);
562  
563 # 380690.82
564  
565 # $float = $random->float(2);
566  
567 # 694.57
568  
569 =back
570  
571 =over 4
572  
573 =item float example 3
574  
575 # given: synopsis
576  
577 package main;
578  
579 my $float = $random->float(2, 1, 5);
580  
581 # 3.98
582  
583 # $float = $random->float(2, 1, 5);
584  
585 # 2.37
586  
587 =back
588  
589 =over 4
590  
591 =item float example 4
592  
593 # given: synopsis
594  
595 package main;
596  
597 my $float = $random->float(3, 1, 2);
598  
599 # 1.745
600  
601 # $float = $random->float(3, 1, 2);
602  
603 # 1.343
604  
605 =back
606  
607 =cut
608  
609 =head2 letter
610  
611 letter() (string)
612  
613 The letter method returns a random letter, which is either an L</uppercased> or
614 L</lowercased> value.
615  
616 I<Since C<1.11>>
617  
618 =over 4
619  
620 =item letter example 1
621  
622 # given: synopsis
623  
624 package main;
625  
626 my $letter = $random->letter;
627  
628 # "i"
629  
630 # $letter = $random->letter;
631  
632 # "K"
633  
634 =back
635  
636 =cut
637  
638 =head2 lowercased
639  
640 lowercased() (string)
641  
642 The lowercased method returns a random lowercased letter.
643  
644 I<Since C<1.11>>
645  
646 =over 4
647  
648 =item lowercased example 1
649  
650 # given: synopsis
651  
652 package main;
653  
654 my $lowercased = $random->lowercased;
655  
656 # "t"
657  
658 # $lowercased = $random->lowercased;
659  
660 # "i"
661  
662 =back
663  
664 =cut
665  
666 =head2 nonzero
667  
668 nonzero(string | coderef $code, any @args) (number | string)
669  
670 The nonzero method dispatches to the specified method or coderef and returns
671 the random value ensuring that it's never zero, not even a percentage of zero.
672 By default, if no arguments are provided, this method dispatches to L</digit>.
673  
674 I<Since C<1.11>>
675  
676 =over 4
677  
678 =item nonzero example 1
679  
680 # given: synopsis
681  
682 package main;
683  
684 my $nonzero = $random->nonzero;
685  
686 # 7
687  
688 # $nonzero = $random->nonzero;
689  
690 # 3
691  
692 =back
693  
694 =over 4
695  
696 =item nonzero example 2
697  
698 # given: synopsis
699  
700 package main;
701  
702 my $nonzero = $random->nonzero("pick");
703  
704 # 1.74452500006101
705  
706 # $nonzero = $random->nonzero("pick");
707  
708 # 1.34270147871891
709  
710 =back
711  
712 =over 4
713  
714 =item nonzero example 3
715  
716 # given: synopsis
717  
718 package main;
719  
720 my $nonzero = $random->nonzero("number");
721  
722 # 3427014
723  
724 # $nonzero = $random->nonzero("number");
725  
726 # 3
727  
728 =back
729  
730 =over 4
731  
732 =item nonzero example 4
733  
734 # given: synopsis
735  
736 package main;
737  
738 my $nonzero = $random->nonzero("number", 0, 10);
739  
740 # 8
741  
742 # $nonzero = $random->nonzero("number", 0, 10);
743  
744 # 3
745  
746 =back
747  
748 =cut
749  
750 =head2 number
751  
752 number(number $from, number $upto) (number)
753  
754 The number method returns a random number within the range provided. If no
755 arguments are provided, the range is from C<0> to C<2147483647>. If only the
756 first argument is provided, it's treated as the desired length of the number.
757  
758 I<Since C<1.11>>
759  
760 =over 4
761  
762 =item number example 1
763  
764 # given: synopsis
765  
766 package main;
767  
768 my $number = $random->number;
769  
770 # 3427014
771  
772 # $number = $random->number;
773  
774 # 3
775  
776 =back
777  
778 =over 4
779  
780 =item number example 2
781  
782 # given: synopsis
783  
784 package main;
785  
786 my $number = $random->number(5, 50);
787  
788 # 39
789  
790 # $number = $random->number(5, 50);
791  
792 # 20
793  
794 =back
795  
796 =over 4
797  
798 =item number example 3
799  
800 # given: synopsis
801  
802 package main;
803  
804 my $number = $random->number(100, 20);
805  
806 # 42
807  
808 # $number = $random->number(100, 20);
809  
810 # 73
811  
812 =back
813  
814 =over 4
815  
816 =item number example 4
817  
818 # given: synopsis
819  
820 package main;
821  
822 my $number = $random->number(5);
823  
824 # 74451
825  
826 # $number = $random->number(5);
827  
828 # 34269
829  
830 =back
831  
832 =cut
833  
834 =head2 pick
835  
836 pick(Num $data) (Num)
837  
838 The pick method is the random number generator and returns a random number. By
839 default, calling this method is equivalent to call L<perlfunc/rand>. This
840 method can be overridden in a subclass to provide a custom generator, e.g. a
841 more cyptographically secure generator.
842  
843 I<Since C<1.23>>
844  
845 =over 4
846  
847 =item pick example 1
848  
849 # given: synopsis
850  
851 package main;
852  
853 my $pick = $random->pick;
854  
855 # 0.744525000061007
856  
857 # $pick = $random->pick;
858  
859 # 0.342701478718908
860  
861 =back
862  
863 =over 4
864  
865 =item pick example 2
866  
867 # given: synopsis
868  
869 package main;
870  
871 my $pick = $random->pick(100);
872  
873 # 74.4525000061007
874  
875 # $pick = $random->pick(100);
876  
877 # 34.2701478718908
878  
879 =back
880  
881 =over 4
882  
883 =item pick example 3
884  
885 # given: synopsis
886  
887 package main;
888  
889 my $pick = $random->pick(2);
890  
891 # 1.48905000012201
892  
893 # $pick = $random->pick(2);
894  
895 # 0.685402957437816
896  
897 =back
898  
899 =cut
900  
901 =head2 range
902  
903 range(string $from, string $to) (number)
904  
905 The range method returns a random number within the range provided. If no
906 arguments are provided, the range is from C<0> to C<2147483647>.
907  
908 I<Since C<1.11>>
909  
910 =over 4
911  
912 =item range example 1
913  
914 # given: synopsis
915  
916 package main;
917  
918 my $range = $random->range(1, 10);
919  
920 # 8
921  
922 # $range = $random->range(1, 10);
923  
924 # 4
925  
926 =back
927  
928 =over 4
929  
930 =item range example 2
931  
932 # given: synopsis
933  
934 package main;
935  
936 my $range = $random->range(10, 1);
937  
938 # 5
939  
940 # $range = $random->range(10, 1);
941  
942 # 8
943  
944 =back
945  
946 =over 4
947  
948 =item range example 3
949  
950 # given: synopsis
951  
952 package main;
953  
954 my $range = $random->range(0, 60);
955  
956 # 45
957  
958 # $range = $random->range(0, 60);
959  
960 # 20
961  
962 =back
963  
964 =over 4
965  
966 =item range example 4
967  
968 # given: synopsis
969  
970 package main;
971  
972 my $range = $random->range(-5, -1);
973  
974 # -2
975  
976 # $range = $random->range(-5, -1);
977  
978 # -4
979  
980 =back
981  
982 =cut
983  
984 =head2 repeat
985  
986 repeat(number $times, string | coderef $code, any @args) (number | string)
987  
988 The repeat method dispatches to the specified method or coderef, repeatedly
989 based on the number of C<$times> specified, and returns the random results from
990 each dispatched call. In list context, the results from each call is returned
991 as a list, in scalar context the results are concatenated.
992  
993 I<Since C<1.11>>
994  
995 =over 4
996  
997 =item repeat example 1
998  
999 # given: synopsis
1000  
1001 package main;
1002  
1003 my @repeat = $random->repeat(2);
1004  
1005 # (7, 3)
1006  
1007 # @repeat = $random->repeat(2);
1008  
1009 # (1, 4)
1010  
1011  
1012  
1013  
1014 =back
1015  
1016 =over 4
1017  
1018 =item repeat example 2
1019  
1020 # given: synopsis
1021  
1022 package main;
1023  
1024 my @repeat = $random->repeat(2, "float");
1025  
1026 # (1447361.5, "0.0000")
1027  
1028 # @repeat = $random->repeat(2, "float");
1029  
1030 # ("482092.1040", 1555.7410393)
1031  
1032  
1033  
1034  
1035 =back
1036  
1037 =over 4
1038  
1039 =item repeat example 3
1040  
1041 # given: synopsis
1042  
1043 package main;
1044  
1045 my @repeat = $random->repeat(2, "character");
1046  
1047 # (")", 4)
1048  
1049 # @repeat = $random->repeat(2, "character");
1050  
1051 # (8, "R")
1052  
1053 =back
1054  
1055 =cut
1056  
1057 =head2 reseed
1058  
1059 reseed(string $seed) (Venus::Random)
1060  
1061 The reseed method sets the L<perlfunc/srand> (i.e. the PRNG seed) to the value
1062 provided, or the default value used on instanstiation when no seed is passed to
1063 the constructor. This method returns the object that invoked it.
1064  
1065 I<Since C<1.11>>
1066  
1067 =over 4
1068  
1069 =item reseed example 1
1070  
1071 # given: synopsis
1072  
1073 package main;
1074  
1075 my $reseed = $random->reseed;
1076  
1077 # bless({value => ...}, "Venus::Random")
1078  
1079 # my $bit = $random->bit;
1080  
1081 # 0
1082  
1083 =back
1084  
1085 =over 4
1086  
1087 =item reseed example 2
1088  
1089 # given: synopsis
1090  
1091 package main;
1092  
1093 my $reseed = $random->reseed(42);
1094  
1095 # bless({value => 42}, "Venus::Random")
1096  
1097 # my $bit = $random->bit;
1098  
1099 # 0
1100  
1101 =back
1102  
1103 =cut
1104  
1105 =head2 reset
1106  
1107 reset() (Venus::Random)
1108  
1109 The reset method sets the L<perlfunc/srand> (i.e. the PRNG seed) to the default
1110 value used on instanstiation when no seed is passed to the constructor. This
1111 method returns the object that invoked it.
1112  
1113 I<Since C<1.11>>
1114  
1115 =over 4
1116  
1117 =item reset example 1
1118  
1119 # given: synopsis
1120  
1121 package main;
1122  
1123 my $reset = $random->reset;
1124  
1125 # bless({value => ...}, "Venus::Random")
1126  
1127 =back
1128  
1129 =cut
1130  
1131 =head2 restore
1132  
1133 restore() (Venus::Random)
1134  
1135 The restore method sets the L<perlfunc/srand> (i.e. the PRNG seed) to the
1136 original value used by L<perlfunc/rand>. This method returns the object that
1137 invoked it.
1138  
1139 I<Since C<1.11>>
1140  
1141 =over 4
1142  
1143 =item restore example 1
1144  
1145 # given: synopsis
1146  
1147 package main;
1148  
1149 my $restore = $random->restore;
1150  
1151 # bless({value => ...}, "Venus::Random")
1152  
1153 =back
1154  
1155 =cut
1156  
1157 =head2 select
1158  
1159 select(arrayref | hashref $data) (any)
1160  
1161 The select method returns a random value from the I<"hashref"> or I<"arrayref">
1162 provided.
1163  
1164 I<Since C<1.11>>
1165  
1166 =over 4
1167  
1168 =item select example 1
1169  
1170 # given: synopsis
1171  
1172 package main;
1173  
1174 my $select = $random->select(["a".."d"]);
1175  
1176 # "c"
1177  
1178 # $select = $random->select(["a".."d"]);
1179  
1180 # "b"
1181  
1182 =back
1183  
1184 =over 4
1185  
1186 =item select example 2
1187  
1188 # given: synopsis
1189  
1190 package main;
1191  
1192 my $select = $random->select({"a".."h"});
1193  
1194 # "f"
1195  
1196 # $select = $random->select({"a".."h"});
1197  
1198 # "d"
1199  
1200 =back
1201  
1202 =cut
1203  
1204 =head2 symbol
1205  
1206 symbol() (string)
1207  
1208 The symbol method returns a random symbol.
1209  
1210 I<Since C<1.11>>
1211  
1212 =over 4
1213  
1214 =item symbol example 1
1215  
1216 # given: synopsis
1217  
1218 package main;
1219  
1220 my $symbol = $random->symbol;
1221  
1222 # "'"
1223  
1224 # $symbol = $random->symbol;
1225  
1226 # ")"
1227  
1228 =back
1229  
1230 =cut
1231  
1232 =head2 uppercased
1233  
1234 uppercased() (string)
1235  
1236 The uppercased method returns a random uppercased letter.
1237  
1238 I<Since C<1.11>>
1239  
1240 =over 4
1241  
1242 =item uppercased example 1
1243  
1244 # given: synopsis
1245  
1246 package main;
1247  
1248 my $uppercased = $random->uppercased;
1249  
1250 # "T"
1251  
1252 # $uppercased = $random->uppercased;
1253  
1254 # "I"
1255  
1256 =back
1257  
1258 =cut
1259  
1260 =head1 AUTHORS
1261  
1262 Awncorp, C<awncorp@cpan.org>
1263  
1264 =cut
1265  
1266 =head1 LICENSE
1267  
1268 Copyright (C) 2000, Awncorp, C<awncorp@cpan.org>.
1269  
1270 This program is free software, you can redistribute it and/or modify it under
1271 the terms of the Apache license version 2.0.
1272  
1273 =cut
« blib/lib/Venus/Os.pm ↑ Summary blib/lib/Venus/Error.pm »