2 * This file is part of gtkD.
4 * gtkD is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
9 * gtkD is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with gtkD; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 // generated automatically - do not change
20 // find conversion definition on APILookup.txt
21 // implement new conversion functionalities on the wrap.utils pakage
24 * Conversion parameters:
25 * inFile = glib-Random-Numbers.html
56 import tango
.io
.Stdout
; // use the tango loging?
60 private import gtkc
.glibtypes
;
62 private import gtkc
.glib
;
71 * The following functions allow you to use a portable, fast and good
72 * pseudo-random number generator (PRNG). It uses the Mersenne Twister
73 * PRNG, which was originally developed by Makoto Matsumoto and Takuji
74 * Nishimura. Further information can be found at www.math.keio.ac.jp/~matumoto/emt.html.
75 * If you just need a random number, you simply call the
76 * g_random_* functions, which will create a globally
77 * used GRand and use the according g_rand_* functions
78 * internally. Whenever you need a stream of reproducible random numbers, you
79 * better create a GRand yourself and use the g_rand_*
80 * functions directly, which will also be slightly faster. Initializing a GRand
81 * with a certain seed will produce exactly the same series of random numbers
82 * on all platforms. This can thus be used as a seed for e.g. games.
83 * The g_rand*_range functions will return high quality
84 * equally distributed random numbers, whereas for example the
85 * (g_random_int()%max) approach often doesn't
86 * yield equally distributed numbers.
87 * GLib changed the seeding algorithm for the pseudo-random number
88 * generator Mersenne Twister, as used by GRand
89 * and GRandom. This was necessary, because some
90 * seeds would yield very bad pseudo-random streams. Also the
91 * pseudo-random integers generated by
92 * g_rand*_int_range() will have a
93 * slightly better equal distribution with the new version of GLib.
94 * The original seeding and generation algorithms, as found in GLib 2.0.x,
95 * can be used instead of the new ones by setting the environment variable
96 * G_RANDOM_VERSION to the value of '2.0'. Use the
97 * GLib-2.0 algorithms only if you have sequences of numbers generated
98 * with Glib-2.0 that you need to reproduce exactly.
103 /** the main Gtk struct */
104 protected GRand
* gRand
;
107 public GRand
* getRandGStruct()
113 /** the main Gtk struct as a void* */
114 protected void* getStruct()
116 return cast(void*)gRand
;
120 * Sets our main struct and passes it to the parent class
122 public this (GRand
* gRand
)
131 Stdout("struct gRand is null on constructor").newline
;
135 printf("struct gRand is null on constructor");
142 assert(gRand
!is null, "struct gRand is null on constructor");
152 * Creates a new random number generator initialized with seed.
154 * a value to initialize the random number generator.
158 public static RandG
randNewWithSeed(uint seed
)
160 // GRand* g_rand_new_with_seed (guint32 seed);
161 return new RandG( g_rand_new_with_seed(seed
) );
165 * Creates a new random number generator initialized with seed.
167 * an array of seeds to initialize the random number generator.
169 * an array of seeds to initialize the random number generator.
174 public static RandG
randNewWithSeedArray(uint* seed
, uint seedLength
)
176 // GRand* g_rand_new_with_seed_array (const guint32 *seed, guint seed_length);
177 return new RandG( g_rand_new_with_seed_array(seed
, seedLength
) );
181 * Creates a new random number generator initialized with a seed taken
182 * either from /dev/urandom (if existing) or from
183 * the current time (as a fallback).
187 public static RandG
randNew()
189 // GRand* g_rand_new (void);
190 return new RandG( g_rand_new() );
194 * Copies a GRand into a new one with the same exact state as before.
195 * This way you can take a snapshot of the random number generator for
203 public RandG
randCopy()
205 // GRand* g_rand_copy (GRand *rand_);
206 return new RandG( g_rand_copy(gRand
) );
210 * Frees the memory allocated for the GRand.
214 public void randFree()
216 // void g_rand_free (GRand *rand_);
221 * Sets the seed for the random number generator GRand to seed.
225 * a value to reinitialize the random number generator.
227 public void randSetSeed(uint seed
)
229 // void g_rand_set_seed (GRand *rand_, guint32 seed);
230 g_rand_set_seed(gRand
, seed
);
234 * Initializes the random number generator by an array of
235 * longs. Array can be of arbitrary size, though only the
236 * first 624 values are taken. This function is useful
237 * if you have many low entropy seeds, or if you require more then
238 * 32bits of actual entropy for your application.
242 * array to initialize with
247 public void randSetSeedArray(uint* seed
, uint seedLength
)
249 // void g_rand_set_seed_array (GRand *rand_, const guint32 *seed, guint seed_length);
250 g_rand_set_seed_array(gRand
, seed
, seedLength
);
255 * Returns the next random guint32 from rand_ equally distributed over
256 * the range [0..2^32-1].
262 public uint randInt()
264 // guint32 g_rand_int (GRand *rand_);
265 return g_rand_int(gRand
);
269 * Returns the next random gint32 from rand_ equally distributed over
270 * the range [begin..end-1].
274 * lower closed bound of the interval.
276 * upper open bound of the interval.
280 public int randIntRange(int begin
, int end
)
282 // gint32 g_rand_int_range (GRand *rand_, gint32 begin, gint32 end);
283 return g_rand_int_range(gRand
, begin
, end
);
287 * Returns the next random gdouble from rand_ equally distributed over
294 public double randDouble()
296 // gdouble g_rand_double (GRand *rand_);
297 return g_rand_double(gRand
);
301 * Returns the next random gdouble from rand_ equally distributed over
302 * the range [begin..end).
306 * lower closed bound of the interval.
308 * upper open bound of the interval.
312 public double randDoubleRange(double begin
, double end
)
314 // gdouble g_rand_double_range (GRand *rand_, gdouble begin, gdouble end);
315 return g_rand_double_range(gRand
, begin
, end
);
319 * Sets the seed for the global random number generator, which is used
320 * by the g_random_* functions, to seed.
322 * a value to reinitialize the global random number generator.
324 public static void randomSetSeed(uint seed
)
326 // void g_random_set_seed (guint32 seed);
327 g_random_set_seed(seed
);
332 * Return a random guint32 equally distributed over the range
337 public static uint randomInt()
339 // guint32 g_random_int (void);
340 return g_random_int();
344 * Returns a random gint32 equally distributed over the range
347 * lower closed bound of the interval.
349 * upper open bound of the interval.
353 public static int randomIntRange(int begin
, int end
)
355 // gint32 g_random_int_range (gint32 begin, gint32 end);
356 return g_random_int_range(begin
, end
);
360 * Returns a random gdouble equally distributed over the range [0..1).
364 public static double randomDouble()
366 // gdouble g_random_double (void);
367 return g_random_double();
371 * Returns a random gdouble equally distributed over the range [begin..end).
373 * lower closed bound of the interval.
375 * upper open bound of the interval.
379 public static double randomDoubleRange(double begin
, double end
)
381 // gdouble g_random_double_range (gdouble begin, gdouble end);
382 return g_random_double_range(begin
, end
);