Math-GMPz
view release on metacpan or search on metacpan
To read from STDIN:
TRmpz_inp_str($rop, *stdin, $base);
To read from an open filehandle (let's call it $fh):
TRmpz_inp_str($rop, \*$fh, $base);
$bytes_read = Rmpz_inp_raw($rop, \*$fh);
Input from filehandle $fh in the format written by Rmpz_out_raw,
and put the result in $rop. Return the number of bytes read, or
if an error occurred, return 0.
#######################
RANDOM NUMBER FUNCTIONS
In the random number functions, @r is an array of
Math::GMPz objects (one for each random number that is
required). $how_many is the number of random numbers you
want and must be equal to scalar(@r). $bits is simply the
number of random bits required. Before calling the random
number functions, $state must be initialised and seeded.
$state = rand_init($op); # $op is the seed.
Initialises and seeds $state, ready for use with the random
number functions. However, $state has not been blessed into
any package, and therefore does not get cleaned up when it
goes out of scope. To avoid memory leaks you therefore need
to call 'rand_clear($state);' once you have finished with it
and before it goes out of scope. Also, it uses the default
algorithm. Consider using the following initialisation and
seeding routines - they provide a choice of algorithm, and
there's no need to call rand_clear() when you've finished with
them.
$state = zgmp_randinit_default();
This is the Math::GMPz interface to the gmp library function
'gmp_randinit_default'.
$state is blessed into package Math::GMPz::Random and will be
automatically cleaned up when it goes out of scope.
Initialize $state with a default algorithm. This will be a
compromise between speed and randomness, and is recommended for
applications with no special requirements. Currently this is
the gmp_randinit_mt function (Mersenne Twister algorithm).
$state = zgmp_randinit_mt();
This is the Math::GMPz interface to the gmp library function
'gmp_randinit_mt'.
Currently identical to zgmp_randinit_default().
$state = zgmp_randinit_lc_2exp($mpz, $ui, $m2exp);
This is the Math::GMPz interface to the gmp library function
'gmp_randinit_lc_2exp'.
$state is blessed into package Math::GMPz::Random and will be
automatically cleaned up when it goes out of scope.
Initialize $state with a linear congruential algorithm
X = ($mpz*X + $ui) mod (2 ** $m2exp). The low bits of X in this
algorithm are not very random. The least significant bit will have a
period no more than 2, and the second bit no more than 4, etc. For
this reason only the high half of each X is actually used.
When a random number of more than m2exp/2 bits is to be generated,
multiple iterations of the recurrence are used and the results
concatenated.
$state = zgmp_randinit_lc_2exp_size($ui);
This is the Math::GMPz interface to the gmp library function
'gmp_randinit_lc_2exp_size'.
$state is blessed into package Math::GMPz::Random and will be
automatically cleaned up when it goes out of scope.
Initialize state for a linear congruential algorithm as per
gmp_randinit_lc_2exp. a, c and m2exp are selected from a table,
chosen so that $ui bits (or more) of each X will be used,
ie. m2exp/2 >= $ui.
If $ui is bigger than the table data provides then the function fails
and dies with an appropriate error message. The maximum value for $ui
currently supported is 128.
$state2 = zgmp_randinit_set($state1);
This is the Math::GMPz interface to the gmp library function
'gmp_randinit_set'.
$state2 is blessed into package Math::GMPz::Random and will be
automatically cleaned up when it goes out of scope.
Initialize $state2 with a copy of the algorithm and state from
$state1.
$state = zgmp_randinit_default_nobless();
$state = zgmp_randinit_mt_nobless();
$state = zgmp_randinit_lc_2exp_nobless($mpz, $ui, $m2exp);
$state2 = zgmp_randinit_set_nobless($state1);
As for the above comparable function, but $state is not blessed into
any package. (Generally not useful - but they're available if you
want them.)
zgmp_randseed($state, $mpz);
zgmp_randseed_ui($state, $ui);
These are the Math::GMPz interfaces to the gmp library functions
'gmp_randseed' and 'gmp_randseed_ui'.
Seed an initialised (but not yet seeded) $state with $mpz/$ui.
$ui = zgmp_urandomb_ui($state, $bits);
This is the Math::GMPz interface to the gmp library function
'gmp_urandomb_ui'.
Return a uniformly distributed random number of $bits bits, ie. in
the range 0 to 2 ** ($bits - 1) inclusive. $bits must be less than or
equal to the number of bits in an unsigned long.
$ui2 = zgmp_urandomm_ui($state, $ui1);
This is the Math::GMPz interface to the gmp library function
'gmp_urandomm_ui'.
Return a uniformly distributed random number in the range 0 to
$ui1 - 1, inclusive.
Rmpz_urandomm(@r, $state, $mpz, $how_many);
Generate $how_many uniform random integers in the range
0 to $op-1, inclusive.
Rmpz_urandomb(@r, $state, $bits, $how_many);
Generate $how_many uniformly distributed random integers
in the range 0 to 2**($bits-1), inclusive.
Rmpz_rrandomb(@r, $state, $bits, $how_many);
Generate $how_many random integers with long strings of
( run in 0.629 second using v1.01-cache-2.11-cpan-71847e10f99 )