public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug math/14561] New: srand() initializing seed for random() function
@ 2012-09-07 23:06 jm3dev at gmail dot com
  2012-09-07 23:10 ` [Bug math/14561] " jm3dev at gmail dot com
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: jm3dev at gmail dot com @ 2012-09-07 23:06 UTC (permalink / raw)
  To: glibc-bugs

http://sourceware.org/bugzilla/show_bug.cgi?id=14561

             Bug #: 14561
           Summary: srand() initializing seed for random() function
           Product: glibc
           Version: 2.12
            Status: NEW
          Severity: normal
          Priority: P2
         Component: math
        AssignedTo: unassigned@sourceware.org
        ReportedBy: jm3dev@gmail.com
    Classification: Unclassified


DESCRIPTION: This problem arises if you mix the use of this two ways of
generating random number: the srand()/rand() way and the srandom()/random()
function family. The issue is that srand() is somehow initializing the seed for
random() function, but it should be initialized *only* with srandom(). The
expected behavior is random() generator not modifying its seed until done by
srandom(), the current behavior is srand() changing that seed.

IMPACT: As happened to me with a scientific code, it could imply a portability
problem as this is working properly (from my humble point of view) in other
implementations of the library (I tested the different behavior between GNU
glibc 2.12 packaged in a Scientific Linux and the BSD/MacOS-Darwin one). I
detected also the problem in GNU implementation of cstdlib with C++.

HOW TO REPRODUCE: It's very easy, this is a test program:

---- START CODE ----
// Test random()
#include <stdio.h>
#include <stdlib.h>

int main() {

  int i;

  srand((unsigned)time(NULL));

  for (i=1;i<11;i++) {
    printf("%g \n",(random() / (double)0x7fffffff));
  }
}
---- CODE END ----

This is what you get with glibc-headers-2.12-1.80:

$ ./rndtest 
0.0940072
0.269949
0.729428
0.48224
0.0592921
0.4699
0.132521
0.973561
0.532405
0.167931
$ ./rndtest 
0.954241
0.686682
0.0374202
0.304717
0.497409
0.197191
0.496588
0.273147
0.345674
0.756533

This is the expected (what you get in the mentioned other implementation of
stdlib.c and cstdlib):

> ./rndtest 
0.840188 
0.394383 
0.783099 
0.79844 
0.911647 
0.197551 
0.335223 
0.76823 
0.277775 
0.55397 
> ./rndtest
0.840188 
0.394383 
0.783099 
0.79844 
0.911647 
0.197551 
0.335223 
0.76823 
0.277775 
0.55397 

Many thanks.

-- 
Configure bugmail: http://sourceware.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2014-06-17  4:41 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-09-07 23:06 [Bug math/14561] New: srand() initializing seed for random() function jm3dev at gmail dot com
2012-09-07 23:10 ` [Bug math/14561] " jm3dev at gmail dot com
2012-09-08 19:06 ` [Bug math/14561] srand() initializes " jm3dev at gmail dot com
2012-10-24 12:48 ` [Bug libc/14561] " jsm28 at gcc dot gnu.org
2012-12-19 20:41 ` neleai at seznam dot cz
2013-06-03 22:49 ` roland at gnu dot org
2014-06-17  4:41 ` fweimer at redhat dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).