Pseudorandom seeding across OSs

James Wanless james at grok.ltd.uk
Sat Aug 27 08:54:50 CEST 2011


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



More information about the gmp-discuss mailing list