Re: strange behavior of random numbers with gtkmm



My problem is not that i do get the same sequence.
My problem is that the first time i get a different sequence than the following
time even though i always use the same seed.
This only happens in a gtkmm application:
- call srand() with fixed value in the GtkWindow constructor
- call rand()  when the 'step' button is pressed
- several presses on the 'step' button yield first sequence:
 1935233081
   645150759
 1041788230
   711017215
- now i press the 'reset' button which calls srand() with the same
seed as used in the constructor
- i repeatedly press 'the step' button
This gives the second sequence which differs from the first sequence;
  1889402444
  1202690506
    785052894
      61279049
Now, whenever i press te reset button, i get the same sequence of
numbers from the 'step' button.
Only when i restart the application i get the "bad" sequence again.

A simple console application always delivers the same sequence (the
same as the second one in my gtkmm app).

@oscar: did you make sure *not* to press the reset button before you
press the 'step' button the first time? So that
the first call to random() follows the srand() call  triggered in the
constructor?

BTW: I have gtkmmmm 2.16.0 and gtk+ 2.16.6

Jody

On Tue, Jun 15, 2010 at 9:03 PM, Eric Wolf <eric boese-wolf eu> wrote:
> Alexey Savartsov <asavartsov gmail com> writes:
>
>> The problem is that you always calling srand with same seed parameter. So you always get same random sequence.
>>
>> Usually, srand called with the current time value like:
>>
>> srand(time(0));
>>
>> May be you need type casting of time() return value - follow compiler warnings there.
>
> Another possibility —at least in linux— is reading some bytes
> from /dev/urandom and use these as seed.
>
> Maybe random/srandom should be used instead of rand, because
> man 3 advertise them as being more random than rand (tm).
> Whatever this means ...
>
> Eric
>


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