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
* [Bug math/14561] srand() initializing seed for random() function
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 ` jm3dev at gmail dot com
2012-09-08 19:06 ` [Bug math/14561] srand() initializes " jm3dev at gmail dot com
` (4 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: jm3dev at gmail dot com @ 2012-09-07 23:10 UTC (permalink / raw)
To: glibc-bugs
http://sourceware.org/bugzilla/show_bug.cgi?id=14561
JM <jm3dev at gmail dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |jm3dev at gmail dot com
--
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
* [Bug math/14561] srand() initializes seed for random() function
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 ` jm3dev at gmail dot com
2012-10-24 12:48 ` [Bug libc/14561] " jsm28 at gcc dot gnu.org
` (3 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: jm3dev at gmail dot com @ 2012-09-08 19:06 UTC (permalink / raw)
To: glibc-bugs
http://sourceware.org/bugzilla/show_bug.cgi?id=14561
JM <jm3dev at gmail dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
Summary|srand() initializing seed |srand() initializes seed
|for random() function |for random() function
--
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
* [Bug libc/14561] srand() initializes seed for random() function
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 ` jsm28 at gcc dot gnu.org
2012-12-19 20:41 ` neleai at seznam dot cz
` (2 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: jsm28 at gcc dot gnu.org @ 2012-10-24 12:48 UTC (permalink / raw)
To: glibc-bugs
http://sourceware.org/bugzilla/show_bug.cgi?id=14561
Joseph Myers <jsm28 at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |drepper.fsp at gmail dot
| |com
Component|math |libc
--
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
* [Bug libc/14561] srand() initializes seed for random() function
2012-09-07 23:06 [Bug math/14561] New: srand() initializing seed for random() function jm3dev at gmail dot com
` (2 preceding siblings ...)
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
5 siblings, 0 replies; 7+ messages in thread
From: neleai at seznam dot cz @ 2012-12-19 20:41 UTC (permalink / raw)
To: glibc-bugs
http://sourceware.org/bugzilla/show_bug.cgi?id=14561
Ondrej Bilka <neleai at seznam dot cz> changed:
What |Removed |Added
----------------------------------------------------------------------------
AssignedTo|unassigned at sourceware |neleai at seznam dot cz
|dot org |
--- Comment #1 from Ondrej Bilka <neleai at seznam dot cz> 2012-12-19 20:40:27 UTC ---
Created attachment 6784
--> http://sourceware.org/bugzilla/attachment.cgi?id=6784
split random seeds
--
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
* [Bug libc/14561] srand() initializes seed for random() function
2012-09-07 23:06 [Bug math/14561] New: srand() initializing seed for random() function jm3dev at gmail dot com
` (3 preceding siblings ...)
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
5 siblings, 0 replies; 7+ messages in thread
From: roland at gnu dot org @ 2013-06-03 22:49 UTC (permalink / raw)
To: glibc-bugs
http://sourceware.org/bugzilla/show_bug.cgi?id=14561
Roland McGrath <roland at gnu dot org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |ASSIGNED
CC| |roland at gnu dot org
--- Comment #2 from Roland McGrath <roland at gnu dot org> ---
Originally the existing behavior was correct (srand/rand are just aliases for
srandom/random). Later versions of 1003.1 started specifying srandom as well
as srand, so this has become nonconforming.
http://pubs.opengroup.org/onlinepubs/9699919799/functions/rand.html
The implementation shall behave as if no function defined in this
volume of POSIX.1-2008 calls rand() or srand().
Note that srandom and random are defined in this volume:
http://pubs.opengroup.org/onlinepubs/9699919799/functions/random.html
--
You are receiving this mail because:
You are on the CC list for the bug.
^ permalink raw reply [flat|nested] 7+ messages in thread
* [Bug libc/14561] srand() initializes seed for random() function
2012-09-07 23:06 [Bug math/14561] New: srand() initializing seed for random() function jm3dev at gmail dot com
` (4 preceding siblings ...)
2013-06-03 22:49 ` roland at gnu dot org
@ 2014-06-17 4:41 ` fweimer at redhat dot com
5 siblings, 0 replies; 7+ messages in thread
From: fweimer at redhat dot com @ 2014-06-17 4:41 UTC (permalink / raw)
To: glibc-bugs
https://sourceware.org/bugzilla/show_bug.cgi?id=14561
Florian Weimer <fweimer at redhat dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |fweimer at redhat dot com
Flags| |security-
--- Comment #3 from Florian Weimer <fweimer at redhat dot com> ---
Not marking as security because these random number generators should not be
used for anything security-related.
--
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).