Re: strange behavior of random numbers with gtkmm
- From: José Alburquerque <jaalburquerque cox net>
- To: jody <jody xha gmail com>
- Cc: gtkmm-list gnome org
- Subject: Re: strange behavior of random numbers with gtkmm
- Date: Thu, 17 Jun 2010 13:31:59 -0400
On Thu, 2010-06-17 at 09:32 +0200, jody wrote:
> Hi
> (i accidentally sent my previous post only to jose, sohere it is again)
>
> @michi:
> 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.
>
> @jose:
> 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?
>
> Jody
>
> 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
> srand(12343271);
> printf("Reset\n");
> }
>
> /*
> * 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);
In your C++ constructor, this line was called before showing your
widgets. If you do the same in C (ie., move the reset call to before
displaying your box, you'll see that the same thing happens (at least it
does for me).
>
> /* run it */
> gtk_main ();
>
> return 0;
> }
> _______________________________________________
> gtkmm-list mailing list
> gtkmm-list gnome org
> http://mail.gnome.org/mailman/listinfo/gtkmm-list
--
José
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]