summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Ryde <user42@zip.com.au>2001-11-26 21:40:55 +0100
committerKevin Ryde <user42@zip.com.au>2001-11-26 21:40:55 +0100
commitdf0a95f7191bfd77f59e8aa95c0ff27fbca3b5e2 (patch)
treea7cf258c7f9ecfa173b4acb1b0222f33fcd24774
parent2f906a6949597389e23c2644e86db1916581e8d7 (diff)
downloadgmp-df0a95f7191bfd77f59e8aa95c0ff27fbca3b5e2.tar.gz
* demos/perl/GMP.pm, demos/perl/GMP.xs: Use new style gmp_randinit's.
-rw-r--r--demos/perl/GMP.pm26
-rw-r--r--demos/perl/GMP.xs23
2 files changed, 27 insertions, 22 deletions
diff --git a/demos/perl/GMP.pm b/demos/perl/GMP.pm
index 78e291c83..23738bb08 100644
--- a/demos/perl/GMP.pm
+++ b/demos/perl/GMP.pm
@@ -408,24 +408,22 @@ C<-E<gt>> style calls work too.
=head2 GMP::Rand
-This class provides objects holding a state and selected method for random
-number generation. C<randstate> will create a new object using a default or
-specified algorithm.
+This class provides objects holding an algorithm and state for random number
+generation. C<randstate> creates a new object, for example,
use GMP::Rand qw(randstate);
- $r = randstate(); # default algorithm
-
-The 'lc' linear congruential algorithm iterates X = (aX + c) mod m, with m a
-power of 2 and a and c chosen from an internal table.
-
- $r = randstate('lc', 64); # 64-bit linear cong
+ $r = randstate();
+ $r = randstate('lc_2exp_size', 64);
+ $r = randstate('lc_2exp', 43840821, 1, 32);
-The 'lc2exp' algorithm is the same, but allows a and c to be specified. a
-can be an mpz, c must be an unsigned integer. For example, to iterate X =
-(123*X+3) mod 2**256, which incidentally would be an extremely non-random
-generator,
+With no parameters this corresponds to the C function
+C<gmp_randinit_default>, and is a compromise between speed and randomness.
+'lc_2exp_size' corresponds to C<gmp_randinit_lc_2exp_size>, and 'lc_2exp'
+corresponds to C<gmp_randinit_lc_2exp>.
- $r = randstate('lc2exp', mpz(123), 3, 256);
+'lc_2exp_size' can fail if the requested size is bigger than the internal
+table provides for, in which case undef is returned. The maximum size
+currently supported is 128. The other forms always succeed.
A randstate can be seeded with an integer or mpz, using the C<seed> method.
/dev/random might be a good source of randomness, or time() or
diff --git a/demos/perl/GMP.xs b/demos/perl/GMP.xs
index de7f42dd0..6c4524bba 100644
--- a/demos/perl/GMP.xs
+++ b/demos/perl/GMP.xs
@@ -2612,18 +2612,15 @@ CODE:
TRACE_ACTIVE ();
if (items == 0)
- gmp_randinit (RETVAL, GMP_RAND_ALG_DEFAULT, 128);
+ {
+ gmp_randinit_default (RETVAL);
+ }
else
{
STRLEN len;
const char *method = SvPV (ST(0), len);
- if (strcmp (method, "lc") == 0)
- {
- if (items != 2)
- goto invalid;
- gmp_randinit (RETVAL, GMP_RAND_ALG_LC, coerce_ulong (ST(1)));
- }
- else if (strncmp (method, "lc2exp", len) == 0)
+ assert (len == strlen (method));
+ if (strcmp (method, "lc_2exp") == 0)
{
if (items != 4)
goto invalid;
@@ -2632,6 +2629,16 @@ CODE:
coerce_ulong (ST(2)),
coerce_ulong (ST(3)));
}
+ else if (strcmp (method, "lc_2exp_size") == 0)
+ {
+ if (items != 2)
+ goto invalid;
+ if (! gmp_randinit_lc_2exp_size (RETVAL, coerce_ulong (ST(1))))
+ {
+ Safefree (RETVAL);
+ XSRETURN_UNDEF;
+ }
+ }
else
{
invalid: