[hitori/deprecation-fixes] generator: Rework random number seeding to not use deprecated functions
- From: Philip Withnall <pwithnall src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [hitori/deprecation-fixes] generator: Rework random number seeding to not use deprecated functions
- Date: Thu, 22 Aug 2019 10:19:35 +0000 (UTC)
commit 49c5615429e7a4a3bfb0ddd08ee10d81fde0cd19
Author: Philip Withnall <withnall endlessm com>
Date: Thu Aug 22 13:18:32 2019 +0300
generator: Rework random number seeding to not use deprecated functions
g_get_current_time() is deprecated in the upcoming GLib release as it’s
not year-2038-safe. Use g_get_real_time() instead and slightly rework
the seeding to use the full range of numbers available.
Signed-off-by: Philip Withnall <withnall endlessm com>
src/generator.c | 12 ++++--------
src/generator.h | 2 +-
src/main.c | 18 +++++++++---------
3 files changed, 14 insertions(+), 18 deletions(-)
---
diff --git a/src/generator.c b/src/generator.c
index aebf7c8..76e88f6 100644
--- a/src/generator.c
+++ b/src/generator.c
@@ -25,7 +25,7 @@
#include "rules.h"
void
-hitori_generate_board (Hitori *hitori, guint new_board_size, gint seed)
+hitori_generate_board (Hitori *hitori, guint new_board_size, guint seed)
{
guchar i;
guint total, old_total;
@@ -34,17 +34,13 @@ hitori_generate_board (Hitori *hitori, guint new_board_size, gint seed)
g_return_if_fail (hitori != NULL);
g_return_if_fail (new_board_size > 0);
- g_return_if_fail (seed >= -1);
/* Seed the random number generator */
- if (seed == -1) {
- GTimeVal time_;
- g_get_current_time (&time_);
- seed = time_.tv_sec + time_.tv_usec;
- }
+ if (seed == 0)
+ seed = g_get_real_time ();
if (hitori->debug)
- g_debug ("Seed value: %d", seed);
+ g_debug ("Seed value: %u", seed);
srand (seed);
diff --git a/src/generator.h b/src/generator.h
index c466ed7..18aee54 100644
--- a/src/generator.h
+++ b/src/generator.h
@@ -24,7 +24,7 @@
G_BEGIN_DECLS
-void hitori_generate_board (Hitori *hitori, guint new_board_size, gint seed);
+void hitori_generate_board (Hitori *hitori, guint new_board_size, guint seed);
G_END_DECLS
diff --git a/src/main.c b/src/main.c
index 4ad4b00..12b6c27 100644
--- a/src/main.c
+++ b/src/main.c
@@ -38,7 +38,7 @@ static gint handle_command_line (GApplication *application, GApplicationCommandL
typedef struct {
/* Command line parameters. */
gboolean debug;
- gint seed;
+ guint seed;
} HitoriApplicationPrivate;
typedef enum {
@@ -69,10 +69,10 @@ hitori_application_class_init (HitoriApplicationClass *klass)
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_SEED,
- g_param_spec_int ("seed",
- "Generation Seed", "Seed controlling generation of
the board.",
- G_MININT, G_MAXINT, -1,
- G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
+ g_param_spec_uint ("seed",
+ "Generation Seed", "Seed controlling generation
of the board.",
+ 0, G_MAXUINT, 0,
+ G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
}
static void
@@ -83,7 +83,7 @@ hitori_application_init (HitoriApplication *self)
priv = hitori_application_get_instance_private (self);
priv->debug = FALSE;
- priv->seed = -1;
+ priv->seed = 0;
}
static void
@@ -117,7 +117,7 @@ get_property (GObject *object, guint property_id, GValue *value, GParamSpec *psp
g_value_set_boolean (value, priv->debug);
break;
case PROP_SEED:
- g_value_set_int (value, priv->seed);
+ g_value_set_uint (value, priv->seed);
break;
default:
/* We don't have any other property... */
@@ -138,7 +138,7 @@ set_property (GObject *object, guint property_id, const GValue *value, GParamSpe
priv->debug = g_value_get_boolean (value);
break;
case PROP_SEED:
- priv->seed = g_value_get_int (value);
+ priv->seed = g_value_get_uint (value);
break;
default:
/* We don't have any other property... */
@@ -302,7 +302,7 @@ hitori_new_game (Hitori *hitori, guint board_size)
{
hitori->made_a_move = FALSE;
- hitori_generate_board (hitori, board_size, -1);
+ hitori_generate_board (hitori, board_size, 0);
hitori_clear_undo_stack (hitori);
gtk_widget_queue_draw (hitori->drawing_area);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]