Weird behaviour of rand()



Hi
As previously posted on this list, i experience strange behaviour of rand()
when using it under gtkmm. The behaviour in question is the value of
the first random number after srand() has been called with a fixed seed.
In about 1 out of 5 cases the second or third number of the sequence
is returned by rand() instead of the first one.
Example:
On my computer, after i call srand(12343271),
 the first few numbers  returned by rand() are
1889402444
1202690506
785052894
61279049
923267781

In my application, after i start it and request a random number
the first value mostly is 1889402444, but every now and then
the first random number to be returned is 1202690506 or
785052894.

I was able to reproduce this behaviour on two systems.

- Linux aim-plankton 2.6.26.8-57.fc8 #1 SMP Thu Dec 18 19:19:45 EST
2008 i686 i686 i386 GNU/Linux
  gcc (GCC) 4.1.2 20070925 (Red Hat 4.1.2-33)
  gnome 2.20.3

- Linux aim-brat 2.6.26.8-57.fc8 #1 SMP Thu Dec 18 19:19:45 EST 2008
i686 athlon i386 GNU/Linux
  g++ (GCC) 4.1.2 20070925 (Red Hat 4.1.2-33)
  gnome 2.20.3

- Linux aim-carola 2.6.18-gentoo-r6 #1 SMP PREEMPT Tue Jan 2 11:55:07
MET 2007 i686 Intel(R) Xeon(TM) CPU 3.20GHz GenuineIntel GNU/Linux
  gcc (GCC) 4.1.2 (Gentoo 4.1.2 p1.0.2)
  gnome 2.22.3

I have reduced my application to a minimal size
while maintaining the strange behaviour, i.e.
if i remove any of the remaining widgets, the behaviour
cannot be reproduced anymore.

To reproduce the behaviour:
- Start the application
- press "reset"
- press "step"
Repeat this procedure several times (between 5 and 10 times)
and the misbehaviour should show. The application has to be restarted.
After the first time "reset" followed by "step" returns the correct first
random number

"Reset" calls srand(12343271), and display the first random number.
Then it creates an object,
in whose constructor srand(12343271) is called again.
"Step" calls a function of the object which simply calls rand() and
returns the number.
If it doesn't match the first random number computed in "reset", the
message "--- first rand mismatch!"
is displayed.



Could anyone test this application as described above to see if he or
she can reproduce the bad behaviour?
If this misbehaviour is shown on other platforms as well, would this
have to be considered a bug?

Thank You
  Jody

Attachment: SV.zip
Description: Zip archive



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]