Tizen 2.1 base
[platform/upstream/glib2.0.git] / glib / grand.c
index 231ed13..0405628 100644 (file)
  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
  * file for a list of people on the GLib Team.  See the ChangeLog
  * files for a list of changes.  These files are distributed with
- * GLib at ftp://ftp.gtk.org/pub/gtk/.  
+ * GLib at ftp://ftp.gtk.org/pub/gtk/.
  */
 
-/* 
+/*
  * MT safe
  */
 
 #include <unistd.h>
 #endif
 
-#include "glib.h"
-#include "gthreadprivate.h"
-#include "galias.h"
+#include "grand.h"
+
+#include "genviron.h"
+#include "gmain.h"
+#include "gmem.h"
+#include "gtestutils.h"
+#include "gthread.h"
 
 #ifdef G_OS_WIN32
 #include <process.h>           /* For getpid() */
 #endif
 
 /**
- * SECTION: random_numbers
+ * SECTION:random_numbers
  * @title: Random Numbers
  * @short_description: pseudo-random number generator
  *
@@ -64,8 +68,8 @@
  * pseudo-random number generator (PRNG). It uses the Mersenne Twister
  * PRNG, which was originally developed by Makoto Matsumoto and Takuji
  * Nishimura. Further information can be found at
- * <ulink url="http://www.math.keio.ac.jp/~matumoto/emt.html">
- * www.math.keio.ac.jp/~matumoto/emt.html</ulink>.
+ * <ulink url="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html">
+ * http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html</ulink>.
  *
  * If you just need a random number, you simply call the
  * <function>g_random_*</function> functions, which will create a
@@ -126,10 +130,10 @@ static GRand* global_random = NULL;
 static guint
 get_random_version (void)
 {
-  static gboolean initialized = FALSE;
+  static gsize initialized = FALSE;
   static guint random_version;
-  
-  if (!initialized)
+
+  if (g_once_init_enter (&initialized))
     {
       const gchar *version_string = g_getenv ("G_RANDOM_VERSION");
       if (!version_string || version_string[0] == '\000' || 
@@ -143,21 +147,12 @@ get_random_version (void)
                     version_string);
          random_version = 22;
        }
-      initialized = TRUE;
+      g_once_init_leave (&initialized, TRUE);
     }
   
   return random_version;
 }
 
-/* This is called from g_thread_init(). It's used to
- * initialize some static data in a threadsafe way.
- */
-void 
-_g_rand_thread_init (void)
-{
-  (void)get_random_version ();
-}
-
 struct _GRand
 {
   guint32 mt[N]; /* the array for the state vector  */
@@ -326,7 +321,7 @@ g_rand_set_seed (GRand* rand, guint32 seed)
       /* [KNUTH 1981, The Art of Computer Programming */
       /*    Vol. 2 (2nd Ed.), pp102]                  */
       
-      if (seed == 0) /* This would make the PRNG procude only zeros */
+      if (seed == 0) /* This would make the PRNG produce only zeros */
        seed = 0x6b842128; /* Just set it to another number */
       
       rand->mt[0]= seed;
@@ -582,7 +577,11 @@ g_rand_double (GRand* rand)
 gdouble 
 g_rand_double_range (GRand* rand, gdouble begin, gdouble end)
 {
-  return g_rand_double (rand) * (end - begin) + begin;
+  gdouble r;
+
+  r = g_rand_double (rand);
+
+  return r * end - (r - 1) * begin;
 }
 
 /**
@@ -694,7 +693,3 @@ g_random_set_seed (guint32 seed)
     g_rand_set_seed (global_random, seed);
   G_UNLOCK (global_random);
 }
-
-
-#define __G_RAND_C__
-#include "galiasdef.c"