Skip to content

Commit

Permalink
rename Primitive::srand to srandom to avoid clash with Perl - #472
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Jun 5, 2024
1 parent 1fe7dbb commit 721a573
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
18 changes: 10 additions & 8 deletions Basic/Primitive/primitive.pd
Original file line number Diff line number Diff line change
Expand Up @@ -2131,7 +2131,7 @@ double pdl_drand(uint64_t *s);
EOH

pp_def(
'srand',
'srandom',
Pars=>'a();',
GenericTypes => ['Q'],
Code => <<'EOF',
Expand All @@ -2147,16 +2147,18 @@ for a number of threads equal to the return-value of
L<PDL::Core/online_cpus>.
As of 2.062, the generator changed from Perl's generator to xoshiro256++
(see L<https://prng.di.unimi.it/>).
Before PDL 2.090, this was called C<srand>, but was renamed to avoid
clashing with Perl's built-in.
=for usage
srand(); # uses current time
srand(5); # fixed number e.g. for testing
srandom(); # uses current time
srandom(5); # fixed number e.g. for testing
EOF
PMCode=>pp_line_numbers(__LINE__, <<'EOD'),
*srand = \&PDL::srand;
sub PDL::srand { PDL::_srand_int($_[0] // PDL::Core::seed()) }
*srandom = \&PDL::srandom;
sub PDL::srandom { PDL::_srandom_int($_[0] // PDL::Core::seed()) }
EOD
);

Expand Down Expand Up @@ -2189,7 +2191,7 @@ excluding 1 itself). The arguments are the same as C<zeroes>
(q.v.) - i.e. one can specify dimensions, types or give
a template.
You can use the PDL function L</srand> to seed the random generator.
You can use the PDL function L</srandom> to seed the random generator.
If it has not been called yet, it will be with the current time.
As of 2.062, the generator changed from Perl's generator to xoshiro256++
(see L<https://prng.di.unimi.it/>).
Expand Down Expand Up @@ -2235,7 +2237,7 @@ This is the uniform distribution between 0 and 1 (excluding both 0 and
1, cf L</random>). The arguments are the same as C<zeroes> (q.v.) -
i.e. one can specify dimensions, types or give a template.
You can use the PDL function L</srand> to seed the random generator.
You can use the PDL function L</srandom> to seed the random generator.
If it has not been called yet, it will be with the current time.
EOF
PMCode=>pp_line_numbers(__LINE__, <<'EOD'),
Expand Down Expand Up @@ -2268,7 +2270,7 @@ This is generated using the math library routine C<ndtri>.
Mean = 0, Stddev = 1
You can use the PDL function L</srand> to seed the random generator.
You can use the PDL function L</srandom> to seed the random generator.
If it has not been called yet, it will be with the current time.
=cut
Expand Down
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- remove obsolete $PP()
- pptest.t now uses build dir to avoid noexec mount problems
- on pdl_destroy, the sv member IV is set to 0 not a random number - thanks @fantasma13 for report
- rename Primitive::srand to srandom to avoid clash with Perl (#472)

2.089 2024-05-12
- fix numerical misbehaviour in histogram etc on i686 (#474) - thanks @sebastic for report
Expand Down
16 changes: 8 additions & 8 deletions t/primitive-random.t
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ TODO: { local $TODO = 'Some CPAN Testers fails for OpenBSD'; subtest 'random' =>
# check that our random functions work with Perl's srand
# local $TODO = ;

subtest 'random and srand' => sub {
srand 5;
subtest 'random and srandom' => sub {
srandom 5;
my $r1 = random 10;
srand 5;
srandom 5;
my $r2 = random 10;
ok( tapprox( $r1, $r2 ), "random and srand" );
ok( tapprox( $r1, $r2 ), "random and srandom" );
};

subtest 'grandom and srand' => sub {
srand 10;
subtest 'grandom and srandom' => sub {
srandom 10;
my $r1 = grandom 10;
srand 10;
srandom 10;
my $r2 = grandom 10;
ok( tapprox( $r1, $r2 ), "grandom and srand" );
ok( tapprox( $r1, $r2 ), "grandom and srandom" );
};
}; }

Expand Down

0 comments on commit 721a573

Please sign in to comment.