alternative to assert
[gtkD.git] / gtkD / src / glib / RandG.d
blob0a170c06f062073c3bbaab7804487d0180a96268
1 /*
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
26 * outPack = glib
27 * outFile = RandG
28 * strct = GRand
29 * realStrct=
30 * ctorStrct=
31 * clss = RandG
32 * interf =
33 * class Code: No
34 * interface Code: No
35 * template for:
36 * extend =
37 * implements:
38 * prefixes:
39 * - g_
40 * omit structs:
41 * omit prefixes:
42 * omit code:
43 * imports:
44 * structWrap:
45 * - GRand* -> RandG
46 * module aliases:
47 * local aliases:
50 module glib.RandG;
52 version(noAssert)
54 version(Tango)
56 import tango.io.Stdout; // use the tango loging?
60 private import gtkc.glibtypes;
62 private import gtkc.glib;
69 /**
70 * Description
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.
100 public class RandG
103 /** the main Gtk struct */
104 protected GRand* gRand;
107 public GRand* getRandGStruct()
109 return gRand;
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)
124 version(noAssert)
126 if ( gRand is null )
128 int zero = 0;
129 version(Tango)
131 Stdout("struct gRand is null on constructor").newline;
133 else
135 printf("struct gRand is null on constructor");
137 zero = zero / zero;
140 else
142 assert(gRand !is null, "struct gRand is null on constructor");
144 this.gRand = gRand;
152 * Creates a new random number generator initialized with seed.
153 * seed:
154 * a value to initialize the random number generator.
155 * Returns:
156 * the new GRand.
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.
166 * seed:
167 * an array of seeds to initialize the random number generator.
168 * seed_length:
169 * an array of seeds to initialize the random number generator.
170 * Returns:
171 * the new GRand.
172 * Since 2.4
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).
184 * Returns:
185 * the new GRand.
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
196 * replaying later.
197 * rand_:
198 * a GRand.
199 * Returns:
200 * the new GRand.
201 * Since 2.4
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.
211 * rand_:
212 * a GRand.
214 public void randFree()
216 // void g_rand_free (GRand *rand_);
217 g_rand_free(gRand);
221 * Sets the seed for the random number generator GRand to seed.
222 * rand_:
223 * a GRand.
224 * 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.
239 * rand_:
240 * a GRand.
241 * seed:
242 * array to initialize with
243 * seed_length:
244 * length of array
245 * Since 2.4
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].
257 * rand_:
258 * a GRand.
259 * Returns:
260 * A random number.
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].
271 * rand_:
272 * a GRand.
273 * begin:
274 * lower closed bound of the interval.
275 * end:
276 * upper open bound of the interval.
277 * Returns:
278 * A random number.
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
288 * the range [0..1).
289 * rand_:
290 * a GRand.
291 * Returns:
292 * A random number.
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).
303 * rand_:
304 * a GRand.
305 * begin:
306 * lower closed bound of the interval.
307 * end:
308 * upper open bound of the interval.
309 * Returns:
310 * A random number.
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.
321 * 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
333 * [0..2^32-1].
334 * Returns:
335 * A random number.
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
345 * [begin..end-1].
346 * begin:
347 * lower closed bound of the interval.
348 * end:
349 * upper open bound of the interval.
350 * Returns:
351 * A random number.
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).
361 * Returns:
362 * A random number.
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).
372 * begin:
373 * lower closed bound of the interval.
374 * end:
375 * upper open bound of the interval.
376 * Returns:
377 * A random number.
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);