[libmath-prime-util-perl] 09/59: Cleanup random_prime a little

Partha P. Mukherjee ppm-guest at moszumanska.debian.org
Thu May 21 18:44:52 UTC 2015


This is an automated email from the git hooks/post-receive script.

ppm-guest pushed a commit to annotated tag v0.10
in repository libmath-prime-util-perl.

commit e6ac88eeac88617c3dedcd137da7dd3777849177
Author: Dana Jacobsen <dana at acm.org>
Date:   Thu Jun 28 15:24:20 2012 -0600

    Cleanup random_prime a little
---
 lib/Math/Prime/Util.pm | 24 +++++++++---------------
 1 file changed, 9 insertions(+), 15 deletions(-)

diff --git a/lib/Math/Prime/Util.pm b/lib/Math/Prime/Util.pm
index 1937264..a6038d7 100644
--- a/lib/Math/Prime/Util.pm
+++ b/lib/Math/Prime/Util.pm
@@ -183,8 +183,8 @@ sub random_prime {
 
   # Note:  I was using rand($range), but Math::Random::MT ignores the argument
   #        instead of following its documentation.
-  my $irandf = (exists &::rand) ? sub { return int(::rand()*shift); }
-                                : sub { return int(rand()*shift); };
+  my $irandf = (defined &::rand) ? sub { return int(::rand()*shift); }
+                                 : sub { return int(rand()*shift); };
 
   if ($high < 30000) {
     # nice deterministic solution, but gets very costly with large values.
@@ -194,20 +194,14 @@ sub random_prime {
     my $rand = $irandf->($irange);
     $prime = nth_prime($li + $rand);
   } else {
-    # random loop
+    # Generate random numbers in the interval until one is prime.
     my $loop_limit = 2000 * 1000;  # To protect against broken rand
-    if ($range <= 4294967295) {
-      do {
-        $prime = $low + $irandf->($range);
-        croak "Random function broken?" if $loop_limit-- < 0;
-      }  while ( !($prime % 2) || !($prime % 3) || !is_prime($prime) );
-    } else {
-      do {
-        my $rand = ( ($irandf->(4294967295) << 32) + $irandf->(4294967295) ) % $range;
-        $prime = $low + $rand;
-        croak "Random function broken?" if $loop_limit-- < 0;
-      }  while ( !($prime % 2) || !($prime % 3) || !is_prime($prime) );
-    }
+    do {
+      my $rand = ($range <= 4294967295) ? $irandf->($range) :
+                 ( ($irandf->(4294967295) << 32) + $irandf->(4294967295) ) % $range;
+      $prime = $low + $rand;
+      croak "Random function broken?" if $loop_limit-- < 0;
+    } while ( !($prime % 2) || !($prime % 3) || !is_prime($prime) );
   }
   return $prime;
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libmath-prime-util-perl.git



More information about the Pkg-perl-cvs-commits mailing list