| 1 | package Array::Sample::SysRand; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||
| 10 | 2 | our $DATE = '2023-06-20'; # DATE | |||||||||||||||||||||||||||
| 11 | 2 | our $DIST = 'Array-Sample-SysRand'; # DIST | |||||||||||||||||||||||||||
| 12 | 2 | our $VERSION = '0.004'; # VERSION | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | our @EXPORT_OK = qw(sample_sysrand); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 44 | ▶ | sub sample_sysrand { | ||||||||||||||||||||||||||
|
Subroutine
sample_sysrand: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 44 | my ($ary, $n, $opts) = @_; | |||||||||||||||||||||||||||
| 18 | 44 | ▶ | $opts //= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts //= {}
MC/DC100%1/1
$opts //= {}
$opts
Truth table100%2/2
A
A= $opts
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 44 | ▶ | return () if $n < 1 || @$ary < 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$n < 1 or @$ary < 1
MC/DC100%2/2
$n < 1 or @$ary < 1
$n < 1
@$ary < 1
Truth table100%3/3
A or B
A= $n < 1B= @$ary < 1
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 41 | my $k = @$ary / $n; | |||||||||||||||||||||||||||
| 23 | 41 | my $idx = rand() * @$ary; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 41 | my @res; | |||||||||||||||||||||||||||
| 26 | 41 | for my $i (1..$n) { | |||||||||||||||||||||||||||
| 27 | 61 | ▶ | push @res, $opts->{pos} ? int($idx) : $ary->[int($idx)]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 61 | $idx += $k; | |||||||||||||||||||||||||||
| 29 | 61 | ▶ | $idx -= @$ary if $idx >= @$ary; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 41 | @res; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 2 | 1; | |||||||||||||||||||||||||||
| 36 | # ABSTRACT: Systematic random sampling from an array | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | __END__ | ||||||||||||||||||||||||||||