public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug math/710] New: initstate() does not save the current position of the previous state array
@ 2005-02-09 16:46 bergner at vnet dot ibm dot com
  2005-02-09 16:49 ` [Bug math/710] " bergner at vnet dot ibm dot com
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: bergner at vnet dot ibm dot com @ 2005-02-09 16:46 UTC (permalink / raw)
  To: glibc-bugs

The initstate() call does not save the current position of the previous state,
so if we switch back with setstate(), the sequence of random numbers generated
id different than if we had never called initstate/setstate.  This works on AIX,
MacOSX and FreeBSD.  The test case is simple:

Peter-Bergners-Computer:~ peter$ cat rand.c 
#include <stdlib.h>
#include <stdio.h>

int main (int argc, char **argv)
{
  int i;

  srandom(1);
  for (i=0; i < 10; i++) {
    printf("%d\n", (int)random());
    if (argc >= 2) {
      /* This should not perturb the random number sequence above */
      char *os, state[128];
      os = initstate (1, state, sizeof(state));
      setstate (os);
    }
  }
  return 0;
}

Expected result:
[bergner@otta bergner]$ gcc rand.c
[bergner@otta bergner]$ ./a.out 
1804289383
846930886
1681692777
1714636915
1957747793
424238335
719885386
1649760492
596516649
1189641421

Incorrect result:
[bergner@otta bergner]$ ./a.out 1
1804289383
940958272
77627160
1361779697
498448585
1782601122
919270010
55938899
1340091435
476760324

Correct result on MacOSX:

Peter-Bergners-Computer:~ peter$ uname -v
Darwin Kernel Version 7.7.0: Sun Nov  7 16:06:51 PST 2004;
root:xnu/xnu-517.9.5.obj~1/RELEASE_PPC 
Peter-Bergners-Computer:~ peter$ gcc rand.c 
Peter-Bergners-Computer:~ peter$ ./a.out 1
1804289383
846930886
1681692777
1714636915
1957747793
424238335
719885386
1649760492
596516649
1189641421

-- 
           Summary: initstate() does not save the current position of the
                    previous state array
           Product: glibc
           Version: 2.3.4
            Status: NEW
          Severity: normal
          Priority: P2
         Component: math
        AssignedTo: aj at suse dot de
        ReportedBy: bergner at vnet dot ibm dot com
                CC: glibc-bugs at sources dot redhat dot com
 GCC build triplet: linux-i686
  GCC host triplet: linux-i686
GCC target triplet: linux-i686


http://sources.redhat.com/bugzilla/show_bug.cgi?id=710

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


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

end of thread, other threads:[~2005-04-06  0:00 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-02-09 16:46 [Bug math/710] New: initstate() does not save the current position of the previous state array bergner at vnet dot ibm dot com
2005-02-09 16:49 ` [Bug math/710] " bergner at vnet dot ibm dot com
2005-02-10  9:40 ` cvs-commit at gcc dot gnu dot org
2005-02-16  4:19 ` roland at gnu dot org
2005-02-16 11:24 ` cvs-commit at gcc dot gnu dot org
2005-03-15 16:32 ` gbeauchesne at mandrakesoft dot com
2005-03-15 16:33 ` gbeauchesne at mandrakesoft dot com
2005-04-06  0:00 ` roland at gnu dot org

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).