Re: strange behavior of random numbers with gtkmm
- From: jody <jody xha gmail com>
- To: gtkmm-list gnome org
- Subject: Re: strange behavior of random numbers with gtkmm
- Date: Tue, 15 Jun 2010 09:39:53 +0200
Shy *bump* ...
Can anybody reproduce this effect?
jody
On Tue, Jun 8, 2010 at 11:27 AM, jody <jody xha gmail com> wrote:
> Hi
> I've found a strange effect in gtkmm programs involving random numbers:
> for a given seed not always produce the same sequence is produced.
>
> I wrote a test program which shows this. There are two buttons in the window,
> 'reset' to set the random seed to a constant value, and 'step' to get a random
> number and print it.
> The reset function is called in the constructor.
>
> This is the output i get when i run the program and do the following:
> - press the step-button 3 times
> - press the reset-button
> - press the step-button 3 times
> - press the reset-button
> - press the step-button 3 times
>
> [jody localhost rtest]$ ./gtkrand
> Reset
> One step: 1851975387
> One step: 1789185830
> One step: 545709798
> Reset
> One step: 1889402444
> One step: 1202690506
> One step: 785052894
> Reset
> One step: 1889402444
> One step: 1202690506
> One step: 785052894
>
> So the first time, the sequence is different, all following ones are
> always the same.
>
> If i do not call srand in the constructor but instead start the
> sequence with the reset-button,
> the first sequence of random numbers is the same as the following.
>
> So my question is: does gtkmm call random numbers somewhere in its code?
> If yes, at what points?
> If no, how can this strange behaviour be explained?
>
> Thank You
> Jody
>
>
> Here is the code
>
> ///// gtkrandwin.h ------------------------------------------------
> #ifndef __GTKRANDWIN_H__
> #define __GTKRANDWIN_H__
>
> #include <gtkmm/window.h>
> #include <gtkmm/button.h>
> #include <gtkmm/box.h>
>
> class gtkrandwin : public Gtk::Window {
> public:
> gtkrandwin();
>
> protected:
>
> Gtk::HBox m_HBox0;
> Gtk::Button m_butReset;
> Gtk::Button m_butStep;
> virtual void reset_action();
> virtual void step_action();
>
> };
>
> #endif
>
>
> ///// gtkrandwin.cpp ------------------------------------------------
> #include <gtkmm/window.h>
> #include <gtkmm/button.h>
> #include <gtkmm/box.h>
>
> #include "gtkrandwin.h"
>
> #define CAPTION_RESET "Reset"
> #define CAPTION_STEP "Step"
>
> //-------------------------------
> // constructor
> //
> gtkrandwin::gtkrandwin()
> : m_HBox0(false, 5),
> m_butReset(CAPTION_RESET),
> m_butStep(CAPTION_STEP) {
>
>
> m_butStep.signal_clicked().connect(sigc::mem_fun(*this,
> >krandwin::step_action) );
> m_butReset.signal_clicked().connect(sigc::mem_fun(*this,
> >krandwin::reset_action) );
>
> m_HBox0.pack_start(m_butReset, Gtk::PACK_EXPAND_WIDGET,5);
> m_HBox0.pack_start(m_butStep, Gtk::PACK_EXPAND_WIDGET,5);
> add(m_HBox0);
>
> reset_action();
> show_all_children();
> }
>
>
> //-----------------------------------------------------------------------------
> // step_action
> // callback for reset action
> //
> void gtkrandwin::reset_action() {
> // one step
> srand(12343271);
> printf("Reset\n");
> }
>
> //-----------------------------------------------------------------------------
> // step_action
> // callback for reset action
> //
> void gtkrandwin::step_action() {
> // one step
> printf("One step: %d\n", rand());
> }
>
>
> ///// gtkrand.cpp ------------------------------------------------
> #include <glibmm.h>
> #include <gtkmm/main.h>
>
> #include "gtkrandwin.h"
>
>
> int main(int iArgC, char *apArgV[]) {
> Gtk::Main kit(iArgC, apArgV);
>
> gtkrandwin m_randwin;
>
> Gtk::Main::run(m_randwin);
>
> return 0;
> }
>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]