This is the mail archive of the glibc-bugs@sources.redhat.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug math/710] New: initstate() does not save the current position of the previous state array


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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]