Skip Menu |

This queue is for tickets about the Math-Random-MT CPAN distribution.

Report information
The Basics
Id: 77343
Status: resolved
Priority: 0/
Queue: Math-Random-MT

People
Owner: Nobody in particular
Requestors: DAMI [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: 1.13
Fixed in: (no value)



Subject: _rand_seed() is constant under Win32
The new algorithm for _rand_seed introduced in v1.13 doesn't work under Win32 (Windows XP, Strawberry perl v5.12.3) : so the sequence of generated random numbers is the same at each execution ! The problem comes from the following lines in _rand_seed : # Seed rand with the same gettimeofday-based formula that is # used in Perl, and return an integer between 0 and 2**32-1. my ($s, $u) = gettimeofday; CORE::srand(1000003*$s+3*$u); because 1000003*$s is too big for a 32-bit int, so the whole thing becomes a float, and then that float is probably truncated or ignored by CORE::srand. Actually, the comment is wrong : the formula above is not exactly thhe same that is used in Perl. In perl's util.c the code is : u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec; where the cast to (U32) is important.
Show quoted text
> The problem comes from the following lines in _rand_seed : > CORE::srand(1000003*$s+3*$u); > > 1000003*$s is too big for a 32-bit int
< [...] Show quoted text
> the cast to (U32) is important.
Thanks for the report DAMI. I could reproduce the issue on a test environment on Windows. You are right that the value passed to srand() is too large (larger than a 32-bit int) and that this causes the same seed to be generated all the time. I am not quite sure why this code works on other platforms. Maybe srand() accepts larger integers on 64 bit platforms? I attempted to fix this, but without success so far. Any idea on how to cast to U32? Florent
Subject: Re: [rt.cpan.org #77343] _rand_seed() is constant under Win32
Date: Tue, 22 May 2012 15:33:35 +0200 (CEST)
To: bug-Math-Random-MT [...] rt.cpan.org
From: laurent.dami [...] free.fr
Show quoted text
----- Mail original ----- De: "Florent Angly via RT" <bug-Math-Random-MT@rt.cpan.org> À: DAMI@cpan.org Envoyé: Mardi 22 Mai 2012 10:03:54 Objet: [rt.cpan.org #77343] _rand_seed() is constant under Win32 <URL: https://rt.cpan.org/Ticket/Display.html?id=77343 > [..]
>I attempted to fix this, but without success so far. Any idea on how to >cast to U32?
You could do my $seed = do {use integer; 1000003*$s+3*$u}; or my $seed = (1000003*$s+3*$u)&0xFFFFFFFF; but in both cases I'm not sure what kind of truncation occurs (maybe what we get is not the least significant bits, and therefore the seed is not random enough). Actually, thinking of it, my advice would be to drop altogether any call to CORE::srand(), because that call might interfere with other layers in the same process that also could use the C-level rand() : think for example of Apache+mod_perl, if the Perl code calls srand(), then some other Apache modules could be altered in their use of random numbers. So if Math::Random::MT needs a seed, it should just call the regular CORE::rand(), and rely on the fact that the Perl compiler already calls srand() automatically ... with proper cros--platform seed values ! Cheers, Laurent Dami
Sending the previous mail has failed. Please contact your admin, they can find more details in the logs.
Show quoted text
> You could do > > my $seed = do {use integer; 1000003*$s+3*$u};
This returned negative numbers. But all the tests passed. Show quoted text
> or > > my $seed = (1000003*$s+3*$u)&0xFFFFFFFF;
This always returned the same seed, 4294967295 (2**31-1, the largest unsigned integer), like some of the other methods I tested. Show quoted text
> So if Math::Random::MT needs a seed, it should just call the regular > CORE::rand()
Good point. This is much more elegant and all tests pass. So, I committed this to the repository. There should be a new version of Math::Random::MT very soon. Thanks for the help. Florent