Solved [solved] strange behaviour: rand() and 2147483647

Hello,

I am wondering if anyone has insights into a small issue I've been facing. I'm coding some mathematical procedures for work, primarily on a Fedora system, but also work on them on my laptop which is running freebsd FreeBSD. But I seem to be getting some strange behaviour from rand(3) on freebsd FreeBSD.

For testing purposes, I generate a random polynomial like this:
Code:
	int k;
	int *ret = (int *)malloc(sizeof(int)*(n+2));
	ret[0] = n;
	for(k=1;k<n+2;k++)
		ret[k] = rand() % p;

When I generate two such polynomials, they are for some reason congruent modulo 2147483647. (2147483647 is the largest prime less than 31 bits long.)

The polynomials are basically sequences of four coefficients, so the two polynomials I get after having seeded with srand(12) are:

A=[905971725, 1007724845, 1775429673, 361239046]
B=[408376053, 218586959, 1593983543, 222910876]

The issue is that 984943658*A mod 2147483647 is equal to B.

Is this just a bad choice of prime, or am I doing something wrong? The same behaviour happens for any other seed. I suppose I just got unlucky?

Edit:
I just realized the prime 0x7fffffff is used in rand(3), so I guess I should just avoid it.

(0x7fffffff = 2147483647)

Matthew
 
From rand(3) manual page:


Code:
RAND(3)                FreeBSD Library Functions Manual                RAND(3)

NAME
     rand, srand, sranddev, rand_r — [U]bad random number generator[/U]
        
...

     For better generator quality, use random(3) or lrand48(3).
 
Back
Top