Pseudorandom seeding across OSs
James Wanless
james at grok.ltd.uk
Sat Aug 27 12:09:03 CEST 2011
...and neither does this:
Desmond:gmp-ecpp james$ ./random2.gmp-5.0.1.intelOSX.64.static -s
1314427612 -v
random seed = 1314427612
error_shift = 1000
precision = 10000
number of rands 1 to generate:
10
2660269372
3034722827
731258692
3968478100
1362695789
4139779128
1762616485
205169438
1403985168
2557611252
First few Hilbert Class Polynomials :
D = -3, dT = 1, T = 1 0
D = -4, dT = 1, T = 1 -1728
D = -7, dT = 1, T = 1 3375
D = -8, dT = 1, T = 1 -8000
D = -11, dT = 1, T = 1 32768
D = -12, dT = 2, T = 1 -54000 0
D = -15, dT = 2, T = 1 191025 -121287375
D = -16, dT = 2, T = 1 -289224 496793088
D = -19, dT = 1, T = 1 884736
D = -20, dT = 2, T = 1 -1264000 -681472000
number of rands 2 to generate:
10
3350980744
2334722197
841841468
2089836918
1200510943
419716733
1220934167
22287637
1937036251
2789986638
[james at localhost atkin]$ ./random2.gmp-5.0.1.fc4.64.static -s
1314427612 -v
random seed = 1314427612
error_shift = 1000
precision = 10000
number of rands 1 to generate:
10
2660269372
3034722827
731258692
3968478100
1362695789
4139779128
1762616485
205169438
1403985168
2557611252
First few Hilbert Class Polynomials :
D = -3, dT = 1, T = 1 0
D = -4, dT = 1, T = 1 -1728
D = -7, dT = 1, T = 1 3375
D = -8, dT = 1, T = 1 -8000
D = -11, dT = 1, T = 1 32768
D = -12, dT = 2, T = 1 -54000 0
D = -15, dT = 2, T = 1 191025 -121287375
D = -16, dT = 2, T = 1 -289224 496793088
D = -19, dT = 1, T = 1 884736
D = -20, dT = 2, T = 1 -1264000 -681472000
number of rands 2 to generate:
10
3350980744
2334722197
841841468
2089836918
1200510943
419716733
1220934167
22287637
1937036251
2789986638
On 27 Aug 2011, at 07:54, James Wanless wrote:
> OK, well the following code _doesn't_ show up the problem:
>
> Desmond:gmp-ecpp james$ ./random1.gmp-5.0.1.intelOSX.64.static -s
> 1314427612
> random seed = 1314427612
> number of rands to generate:
> 10
> 2660269372
> 3034722827
> 731258692
> 3968478100
> 1362695789
> 4139779128
> 1762616485
> 205169438
> 1403985168
> 2557611252
>
> [james at localhost atkin]$ ./random1.gmp-5.0.1.fc4.64.static -s
> 1314427612
> random seed = 1314427612
> number of rands to generate:
> 10
> 2660269372
> 3034722827
> 731258692
> 3968478100
> 1362695789
> 4139779128
> 1762616485
> 205169438
> 1403985168
> 2557611252
>
>
> /*
> Author: James Wanless (c) 2011
>
> GMP random functionality test across OSs.
>
> */
>
> #include <getopt.h>
> #include <math.h>
> #include <stdio.h>
> #include <stdlib.h>
>
> #include <sys/time.h>
> #include <time.h>
>
> #include <iostream>
> #include <gmpxx.h>
> #include <gmp.h>
>
> using namespace std;
>
>
> gmp_randstate_t rstate;
>
> long seed = 0;
>
> mpz_class rand2()
> /* returns GMP pseudo-random number of 32-bits */
> {
> mpz_t temp;
> mpz_init(temp);
> mpz_urandomb(temp, rstate, 32L);
> mpz_class temp_class(temp);
> mpz_clear(temp);
> return temp_class;
> }
>
> const char* program_name;
>
> void print_usage (FILE* stream, int exit_code)
> {
> fprintf(stream, "Usage: %s options [ < inputfile ]\n", program_name);
> fprintf(stream,
> " -h --help Display this usage information.\n"
> " -s --seed Set (pseudo-)random seed.\n");
> exit(exit_code);
> }
>
>
>
> int main(int argc, char* argv[])
> {
> int next_option;
>
> const char* const short_options = "hs:";
> const struct option long_options[] = {
> {"help", 0, NULL, 'h'},
> {"seed", 1, NULL, 's'},
> {NULL,0,NULL,0}
> };
>
> program_name = argv[0];
>
> do {
> next_option = getopt_long(argc, argv, short_options, long_options,
> NULL);
> switch (next_option)
> {
> case 'h':
> print_usage(stdout, 0);
>
> case 's':
> seed = atol(optarg);
> break;
>
> case '?':
> print_usage(stderr, 1);
>
> case -1:
> break;
>
> default:
> abort();
> }
> }
> while (next_option != -1);
>
> gmp_randinit_default (rstate);
>
> {
> #if HAVE_GETTIMEOFDAY
> struct timeval tv;
> gettimeofday (&tv, NULL);
> if (!seed)
> seed = tv.tv_sec + tv.tv_usec;
>
> #else
> time_t t;
> time (&t);
> if (!seed)
> seed = t;
> #endif
> }
>
> gmp_randseed_ui (rstate, seed);
>
> cout << "random seed = " << seed << "\n";
> fflush(stdout);
>
> mpz_class N;
>
>
>
> cout << "number of rands to generate:\n";
> fflush(stdout);
> cin >> N;
> for (long j=0; j<N; j++)
> cout << rand2() << "\n";
> fflush(stdout);
>
> return 0;
> }
>
>
>
> On 26 Aug 2011, at 23:10, Pedro Gimeno wrote:
>
>> James Wanless wrote:
>>> Thanks very much for your response...
>>> It's actually an (attempted) ECPP output, but my query (I suppose :)
>>> more specifically was wondering why the output shown is different
>>> for
>>> (AFAIK) identical inputs/code/libraries/library-versions/random
>>> seeds
>>> across the two different OSs. Maybe I'm being optimistic hoping
>>> output
>>> would ever be the same for the two - any light you could shed on
>>> this
>>> would be very gratefully received (I don't _think_ there are any
>>> obvious
>>> bugs remnant in the code itself, but...?)
>>
>> The random numbers should be the same regardless of arch. Please see
>> http://www.gmplib.org/list-archives/gmp-devel/2010-September/001642.html
>> (and the rest of the thread if interested).
>>
>> If that's not the case, you might have found a bug in GMP. Please
>> read
>> this guide on how to report it:
>> http://www.gmplib.org/manual/Reporting-Bugs.html
>>
>> The most important part is to include a self-contained test case that
>> illustrates the problem.
>>
>> Thank you.
>> -- Pedro Gimeno
>
> _______________________________________________
> gmp-discuss mailing list
> gmp-discuss at gmplib.org
> https://gmplib.org/mailman/listinfo/gmp-discuss
More information about the gmp-discuss
mailing list