Re: strange behavior of random numbers with gtkmm

(i accidentally sent my previous post only to jose, sohere it is again)

I do some simulations where "randomness" plays a major role.
Sometimes some special events occur, and then it is advantageous
to be able to repeat the simulation the exact same way up to
this occurrence to understand how it happened.
Of course i could do  a srand just before starting the simulation
(instead of inside the WIndow constructor) to ensure this reproducibility.
But still i would like to understand what exactly is going on.

I built an equivalent C version of my test application (see below), and this one
does not show this behavior

Just a thought: if i understand things correctly, there are several active
threads in a gtkmm application. Is it possible that the thread in which
the constructor is called is not the same one which the user events are handled?
If this is the case, it could be that in the user event thread the
state of the random engine is different from the its state in the main thread
(the one with the constructor)?
Or is the state of rand global to the entire process?
But then again why does this not happen with the gtk-version?


Here's the code of the gtk-version

#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>

 * reset_action
 *   callback for reset action
static void reset_action( GtkWidget *widget,
                          gpointer   data ) {
    // one step

 * step_action
 *   callback for reset action
static void step_action( GtkWidget *widget,
                         gpointer   data ) {
    // one step
    printf("One step: %d\n", rand());

static gboolean delete_event( GtkWidget *widget,
                              GdkEvent  *event,
                              gpointer   data )

    g_print ("delete event occurred\n");

    /* Change TRUE to FALSE and the main window will be destroyed with
     * a "delete-event". */

    return FALSE;

/* Another callback */
static void destroy( GtkWidget *widget,
                     gpointer   data )
    gtk_main_quit ();

int main( int   argc,
          char *argv[] )
    /* GtkWidget is the storage type for widgets */
    GtkWidget *window;
    GtkWidget *box1;
    GtkWidget *buttonStep;
    GtkWidget *buttonReset;

    gtk_init (&argc, &argv);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

    g_signal_connect (window, "delete-event",
		      G_CALLBACK (delete_event), NULL);

    g_signal_connect (window, "destroy",
		      G_CALLBACK (destroy), NULL);

    gtk_container_set_border_width (GTK_CONTAINER (window), 10);

    /* create and insert the box */
    box1 = gtk_hbox_new (FALSE, 0);
    gtk_container_add (GTK_CONTAINER (window), box1);

    /* create and insert "reset" button */
    buttonReset = gtk_button_new_with_label ("Reset");

    g_signal_connect (buttonReset, "clicked",
		      G_CALLBACK (reset_action), NULL);

    gtk_box_pack_start (GTK_BOX(box1), buttonReset, TRUE, TRUE, 0);
    gtk_widget_show (buttonReset);

    /* create and insert "step" button */
    buttonStep = gtk_button_new_with_label ("Step");

    g_signal_connect (buttonStep, "clicked",
		      G_CALLBACK (step_action), NULL);

    gtk_box_pack_start (GTK_BOX(box1), buttonStep, TRUE, TRUE, 0);
    gtk_widget_show (buttonStep);

    /* display box */
    gtk_widget_show (box1);

    /* display the window */
    gtk_widget_show (window);

    reset_action(NULL, NULL);

    /* run it */
    gtk_main ();

    return 0;

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