Math-GMPz
view release on metacpan or search on metacpan
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,
demos/solitaire.p view on Meta::CPAN
# perl solitaire.pl X - where X is the number of cards you're playing with. #
# If you want to see the probabilities for all numbers of cards up to and #
# including X, just run: perl solitaire.pl X all #
# NOTE: X must be greater than 1. #
# #
# Turns out that the probability of winning doesn't change much as the number #
# of cards is increased beyond about 5. As the number of cards increases, the #
# probability of "winning" gets closer and closer to 1 in e, where e is the #
# euler number (2.71828...) #
# With each iteration of the for{} loop (below) we get closer and closer to #
# the actual value of e. Furthermore, with successive iterations of the for{} #
# loop, the values alternate between "less than e" and "greater than e". #
# Hence, to maximize your chances of "winning", always play with an even #
# number of cards. With an even number of cards, your chances of winning are #
# always better than 1 in e, whereas with an odd number of cards your chances #
# are always less than 1 in e. (Of course, the difference is quite miniscule.) #
# #
# The same for{} loop can be also used to calculate the euler number to a #
# specified precision. See demos/euler.p in the Math::MPFR source distro. #
# #
# When played with a full deck of 52 standard playing cards, the game is known #
t/imp_exp.t view on Meta::CPAN
use Test::More;
use Config;
print "# Using gmp version ", Math::GMPz::gmp_v(), "\n";
my $z = Math::GMPz->new();
my $z_up = Math::GMPz->new();
my $z_down = Math::GMPz->new();
my $z_check = Math::GMPz->new();
my $iterations = 500;
my $s = "\xf4\x57\xbc\x2b\xaf\xb7\x3f\x2b\x41\x43\xe9\x3f\x3f\x2b\xc5\x52\x48\x90";
my ($order, $size, $endian, $nails) = (1, 1, 0, 0);
# $s contains no ordinal values greater than 0xff.
# Therefore utf8::is_utf8($s) should be false.
cmp_ok(utf8::is_utf8($s), '==', 0, "string is not utf8");
Rmpz_import($z, length($s), $order, $size, $endian, $nails, $s);
t/imp_exp.t view on Meta::CPAN
cmp_ok($z, '==', $val_check, "Rmpz_import_UV basic sanity check");
my @ret = Rmpz_export_UV(0, $Config{ivsize}, 0, 0, $z);
cmp_ok(scalar(@ret), '==', scalar(@uv), "returned array is of expected size");
cmp_ok($ret[0], '==', $uv[0], "1st array elements match");
cmp_ok($ret[1], '==', $uv[1], "2nd array elements match");
cmp_ok($ret[2], '==', $uv[2], "3rd array elements match");
cmp_ok($ret[3], '==', $uv[3], "4th array elements match");
for(1 .. $iterations) {
my ($s, $ords) = randstr(0); # These strings are normal ASCII strings, with all
# characters in the range \x00 .. \x7f.
# Makes no difference to Rmpz_import whether they
# have been upgraded to UTF8 or not.
# utf8::upgrade($s);
my $len = length($s);
Rmpz_import($z, $len, 1, 1, 0, 0, $s);
# Rmpz_out_str($z, 16);
# print("\n");
t/imp_exp.t view on Meta::CPAN
Rmpz_import($z_check, $len, 1, 1, 0, 0, $s_check);
cmp_ok($len, '==', 3, "length of original string (@$ords) is 3");
cmp_ok($s, 'eq', $s_check, "strings match");
cmp_ok($z, '==', $z_check, "values match");
cmp_ok(utf8::is_utf8($s), '==', 0, "string is NOT UTF8");
}
set_globals_to_default();
for(1 .. $iterations) {
my ($s, $ords) = randstr(0); # These strings are normal ASCII strings, with all
# characters in the range \x00 .. \x7f.
# Makes no difference to Rmpz_import whether they
# have been upgraded to UTF8 or not.
$Math::GMPz::utf8_no_warn = 1; # Don't warn about utf8 strings
utf8::upgrade($s);
my $len = length($s);
t/imp_exp.t view on Meta::CPAN
Rmpz_import($z_check, $len, 1, 1, 0, 0, $s_check);
cmp_ok($len, '==', 3, "length of original string (@$ords) is 3");
cmp_ok($s, 'eq', $s_check, "strings match");
cmp_ok($z, '==', $z_check, "values match");
cmp_ok(utf8::is_utf8($s), '!=', 0, "string is UTF8");
}
set_globals_to_default();
for(1 .. $iterations) {
my ($s, $ords) = randstr(1); # These strings contain at least one character
# in the range \x80 .. \xff, and Rmpz_import
# will treat them differently, depending upon
# their UTF8 status.
# utf8::upgrade($s);
my $len = length($s);
Rmpz_import($z, $len, 1, 1, 0, 0, $s);
# Rmpz_out_str($z, 16);
t/imp_exp.t view on Meta::CPAN
Rmpz_import($z_check, $len, 1, 1, 0, 0, $s_check);
cmp_ok($len, '==', 3, "length of original string (@$ords) is 3");
cmp_ok($s, 'eq', $s_check, "strings match");
cmp_ok($z, '==', $z_check, "values match");
cmp_ok(utf8::is_utf8($s), '==', 0, "string is NOT UTF8");
}
set_globals_to_default();
for(1 .. $iterations) {
my ($s, $ords) = randstr(1); # These strings contain at least one character
# in the range \x80 .. \xff, and Rmpz_import
# will treat them differently, depending upon
# their UTF8 status.
$Math::GMPz::utf8_no_warn = 1; # Don't warn about utf8 strings
utf8::upgrade($s);
my $len = length($s);
Rmpz_import($z, $len, 1, 1, 0, 0, $s);
t/imp_exp.t view on Meta::CPAN
cmp_ok($s, 'eq', $s_check, "strings match");
cmp_ok($z, '==', $z_check, "values match");
cmp_ok(utf8::is_utf8($s), '!=', 0, "string is UTF8");
}
set_globals_to_default();
$Math::GMPz::utf8_no_warn = 1; # Don't warn about utf8 strings
$Math::GMPz::utf8_no_downgrade = 1; # Don't perform utf8 downgrade
for(1 .. $iterations) {
my ($s, $ords) = randstr(1);
my @o = @$ords;
utf8::upgrade($s);
my $len = length($s);
Rmpz_import($z, $len, 1, 1, 0, 0, $s);
my $s_check = Rmpz_export(1, 1, 0, 0, $z);
Rmpz_import($z_check, $len, 1, 1, 0, 0, $s_check);
cmp_ok($len, '==', 3, "length of original string (@o) is 3");
t/imp_exp.t view on Meta::CPAN
cmp_ok($z, '==', $z_check, "values match");
cmp_ok(utf8::is_utf8($s), '!=', 0, "string is UTF8");
}
set_globals_to_default();
$Math::GMPz::utf8_no_warn = 1; # Don't warn about utf8 strings
$Math::GMPz::utf8_no_downgrade = 1; # Don't attempt to downgrade as
# it will inevitably fail.
for(1 .. $iterations) {
my ($s, $ords) = randstr(2); # These strings are automatically UTF8, containing
# at least one character greater than \xff.
# Therefore, they cannot be downgraded.
# utf8::upgrade($s); # not needed, already utf8 - but let's check:
cmp_ok(utf8::is_utf8($s), '!=', 0, "string is UTF8");
my $len = length($s);
Rmpz_import($z, $len, 1, 1, 0, 0, $s);
my $s_check = Rmpz_export(1, 1, 0, 0, $z);
( run in 2.939 seconds using v1.01-cache-2.11-cpan-71847e10f99 )