1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19  
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 /*
25  * Conversion parameters:
26  * inFile  = glib-Random-Numbers.html
27  * outPack = glib
28  * outFile = RandG
29  * strct   = GRand
30  * realStrct=
31  * ctorStrct=
32  * clss    = RandG
33  * interf  = 
34  * class Code: Yes
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- g_rand_
41  * 	- g_
42  * omit structs:
43  * omit prefixes:
44  * omit code:
45  * omit signals:
46  * imports:
47  * 	- gtkc.Loader
48  * 	- gtkc.paths
49  * structWrap:
50  * 	- GRand* -> RandG
51  * module aliases:
52  * local aliases:
53  * 	- double -> randDouble
54  * 	- doubleRange -> randDoubleRange
55  * 	- int -> randInt
56  * 	- intRange -> randIntRange
57  * overrides:
58  */
59 
60 module glib.RandG;
61 
62 public  import gtkc.glibtypes;
63 
64 private import gtkc.glib;
65 private import glib.ConstructionException;
66 
67 
68 private import gtkc.Loader;
69 private import gtkc.paths;
70 
71 
72 
73 
74 /**
75  * The following functions allow you to use a portable, fast and good
76  * pseudo-random number generator (PRNG). It uses the Mersenne Twister
77  * PRNG, which was originally developed by Makoto Matsumoto and Takuji
78  * Nishimura. Further information can be found at
79  *
80  * http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html.
81  *
82  * If you just need a random number, you simply call the
83  * g_random_* functions, which will create a
84  * globally used GRand and use the according
85  * g_rand_* functions internally. Whenever you
86  * need a stream of reproducible random numbers, you better create a
87  * GRand yourself and use the g_rand_* functions
88  * directly, which will also be slightly faster. Initializing a GRand
89  * with a certain seed will produce exactly the same series of random
90  * numbers on all platforms. This can thus be used as a seed for e.g.
91  * games.
92  *
93  * The g_rand*_range functions will return high
94  * quality equally distributed random numbers, whereas for example the
95  * (g_random_int()%max) approach often
96  * doesn't yield equally distributed numbers.
97  *
98  * GLib changed the seeding algorithm for the pseudo-random number
99  * generator Mersenne Twister, as used by
100  * GRand and GRandom.
101  * This was necessary, because some seeds would yield very bad
102  * pseudo-random streams. Also the pseudo-random integers generated by
103  * g_rand*_int_range() will have a slightly better
104  * equal distribution with the new version of GLib.
105  *
106  * The original seeding and generation algorithms, as found in GLib
107  * 2.0.x, can be used instead of the new ones by setting the
108  * environment variable G_RANDOM_VERSION to the value of
109  * '2.0'. Use the GLib-2.0 algorithms only if you have sequences of
110  * numbers generated with Glib-2.0 that you need to reproduce exactly.
111  */
112 public class RandG
113 {
114 	
115 	/** the main Gtk struct */
116 	protected GRand* gRand;
117 	
118 	
119 	public GRand* getRandGStruct()
120 	{
121 		return gRand;
122 	}
123 	
124 	
125 	/** the main Gtk struct as a void* */
126 	protected void* getStruct()
127 	{
128 		return cast(void*)gRand;
129 	}
130 	
131 	/**
132 	 * Sets our main struct and passes it to the parent class
133 	 */
134 	public this (GRand* gRand)
135 	{
136 		this.gRand = gRand;
137 	}
138 	
139 	~this ()
140 	{
141 		if ( Linker.isLoaded(LIBRARY.GLIB) && gRand !is null )
142 		{
143 			g_rand_free(gRand);
144 		}
145 	}
146 	
147 	/**
148 	 */
149 	
150 	/**
151 	 * Creates a new random number generator initialized with seed.
152 	 * Params:
153 	 * seed = a value to initialize the random number generator.
154 	 * Throws: ConstructionException GTK+ fails to create the object.
155 	 */
156 	public this (uint seed)
157 	{
158 		// GRand * g_rand_new_with_seed (guint32 seed);
159 		auto p = g_rand_new_with_seed(seed);
160 		if(p is null)
161 		{
162 			throw new ConstructionException("null returned by g_rand_new_with_seed(seed)");
163 		}
164 		this(cast(GRand*) p);
165 	}
166 	
167 	/**
168 	 * Creates a new random number generator initialized with seed.
169 	 * Since 2.4
170 	 * Params:
171 	 * seed = an array of seeds to initialize the random number generator.
172 	 * Throws: ConstructionException GTK+ fails to create the object.
173 	 */
174 	public this (uint[] seed)
175 	{
176 		// GRand * g_rand_new_with_seed_array (const guint32 *seed,  guint seed_length);
177 		auto p = g_rand_new_with_seed_array(seed.ptr, cast(int) seed.length);
178 		if(p is null)
179 		{
180 			throw new ConstructionException("null returned by g_rand_new_with_seed_array(seed.ptr, cast(int) seed.length)");
181 		}
182 		this(cast(GRand*) p);
183 	}
184 	
185 	/**
186 	 * Creates a new random number generator initialized with a seed taken
187 	 * either from /dev/urandom (if existing) or from
188 	 * the current time (as a fallback).
189 	 * Throws: ConstructionException GTK+ fails to create the object.
190 	 */
191 	public this ()
192 	{
193 		// GRand * g_rand_new (void);
194 		auto p = g_rand_new();
195 		if(p is null)
196 		{
197 			throw new ConstructionException("null returned by g_rand_new()");
198 		}
199 		this(cast(GRand*) p);
200 	}
201 	
202 	/**
203 	 * Copies a GRand into a new one with the same exact state as before.
204 	 * This way you can take a snapshot of the random number generator for
205 	 * replaying later.
206 	 * Since 2.4
207 	 * Returns: the new GRand.
208 	 */
209 	public RandG copy()
210 	{
211 		// GRand * g_rand_copy (GRand *rand_);
212 		auto p = g_rand_copy(gRand);
213 		
214 		if(p is null)
215 		{
216 			return null;
217 		}
218 		
219 		return new RandG(cast(GRand*) p);
220 	}
221 	
222 	/**
223 	 * Frees the memory allocated for the GRand.
224 	 */
225 	public void free()
226 	{
227 		// void g_rand_free (GRand *rand_);
228 		g_rand_free(gRand);
229 	}
230 	
231 	/**
232 	 * Sets the seed for the random number generator GRand to seed.
233 	 * Params:
234 	 * seed = a value to reinitialize the random number generator.
235 	 */
236 	public void setSeed(uint seed)
237 	{
238 		// void g_rand_set_seed (GRand *rand_,  guint32 seed);
239 		g_rand_set_seed(gRand, seed);
240 	}
241 	
242 	/**
243 	 * Initializes the random number generator by an array of
244 	 * longs. Array can be of arbitrary size, though only the
245 	 * first 624 values are taken. This function is useful
246 	 * if you have many low entropy seeds, or if you require more then
247 	 * 32bits of actual entropy for your application.
248 	 * Since 2.4
249 	 * Params:
250 	 * seed = array to initialize with
251 	 */
252 	public void setSeedArray(uint[] seed)
253 	{
254 		// void g_rand_set_seed_array (GRand *rand_,  const guint32 *seed,  guint seed_length);
255 		g_rand_set_seed_array(gRand, seed.ptr, cast(int) seed.length);
256 	}
257 	
258 	/**
259 	 * Returns the next random guint32 from rand_ equally distributed over
260 	 * the range [0..2^32-1].
261 	 * Params:
262 	 * rand = a GRand.
263 	 * Returns: A random number.
264 	 */
265 	public uint randInt()
266 	{
267 		// guint32 g_rand_int (GRand *rand_);
268 		return g_rand_int(gRand);
269 	}
270 	
271 	/**
272 	 * Returns the next random gint32 from rand_ equally distributed over
273 	 * the range [begin..end-1].
274 	 * Params:
275 	 * rand = a GRand.
276 	 * begin = lower closed bound of the interval.
277 	 * end = upper open bound of the interval.
278 	 * Returns: A random number.
279 	 */
280 	public int randIntRange(int begin, int end)
281 	{
282 		// gint32 g_rand_int_range (GRand *rand_,  gint32 begin,  gint32 end);
283 		return g_rand_int_range(gRand, begin, end);
284 	}
285 	
286 	/**
287 	 * Returns the next random gdouble from rand_ equally distributed over
288 	 * the range [0..1).
289 	 * Params:
290 	 * rand = a GRand.
291 	 * Returns: A random number.
292 	 */
293 	public double randDouble()
294 	{
295 		// gdouble g_rand_double (GRand *rand_);
296 		return g_rand_double(gRand);
297 	}
298 	
299 	/**
300 	 * Returns the next random gdouble from rand_ equally distributed over
301 	 * the range [begin..end).
302 	 * Params:
303 	 * rand = a GRand.
304 	 * begin = lower closed bound of the interval.
305 	 * end = upper open bound of the interval.
306 	 * Returns: A random number.
307 	 */
308 	public double randDoubleRange(double begin, double end)
309 	{
310 		// gdouble g_rand_double_range (GRand *rand_,  gdouble begin,  gdouble end);
311 		return g_rand_double_range(gRand, begin, end);
312 	}
313 	
314 	/**
315 	 * Sets the seed for the global random number generator, which is used
316 	 * by the g_random_* functions, to seed.
317 	 * Params:
318 	 * seed = a value to reinitialize the global random number generator.
319 	 */
320 	public static void randomSetSeed(uint seed)
321 	{
322 		// void g_random_set_seed (guint32 seed);
323 		g_random_set_seed(seed);
324 	}
325 	
326 	/**
327 	 * Return a random guint32 equally distributed over the range
328 	 * [0..2^32-1].
329 	 * Returns: A random number.
330 	 */
331 	public static uint randomInt()
332 	{
333 		// guint32 g_random_int (void);
334 		return g_random_int();
335 	}
336 	
337 	/**
338 	 * Returns a random gint32 equally distributed over the range
339 	 * [begin..end-1].
340 	 * Params:
341 	 * begin = lower closed bound of the interval.
342 	 * end = upper open bound of the interval.
343 	 * Returns: A random number.
344 	 */
345 	public static int randomIntRange(int begin, int end)
346 	{
347 		// gint32 g_random_int_range (gint32 begin,  gint32 end);
348 		return g_random_int_range(begin, end);
349 	}
350 	
351 	/**
352 	 * Returns a random gdouble equally distributed over the range [0..1).
353 	 * Returns: A random number.
354 	 */
355 	public static double randomDouble()
356 	{
357 		// gdouble g_random_double (void);
358 		return g_random_double();
359 	}
360 	
361 	/**
362 	 * Returns a random gdouble equally distributed over the range [begin..end).
363 	 * Params:
364 	 * begin = lower closed bound of the interval.
365 	 * end = upper open bound of the interval.
366 	 * Returns: A random number.
367 	 */
368 	public static double randomDoubleRange(double begin, double end)
369 	{
370 		// gdouble g_random_double_range (gdouble begin,  gdouble end);
371 		return g_random_double_range(begin, end);
372 	}
373 }