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 module glib.RandG;
26 
27 private import glib.ConstructionException;
28 private import gtkc.glib;
29 public  import gtkc.glibtypes;
30 private import gtkd.Loader;
31 
32 
33 /**
34  * The GRand struct is an opaque data structure. It should only be
35  * accessed through the g_rand_* functions.
36  */
37 public class RandG
38 {
39 	/** the main Gtk struct */
40 	protected GRand* gRand;
41 	protected bool ownedRef;
42 
43 	/** Get the main Gtk struct */
44 	public GRand* getRandGStruct(bool transferOwnership = false)
45 	{
46 		if (transferOwnership)
47 			ownedRef = false;
48 		return gRand;
49 	}
50 
51 	/** the main Gtk struct as a void* */
52 	protected void* getStruct()
53 	{
54 		return cast(void*)gRand;
55 	}
56 
57 	/**
58 	 * Sets our main struct and passes it to the parent class.
59 	 */
60 	public this (GRand* gRand, bool ownedRef = false)
61 	{
62 		this.gRand = gRand;
63 		this.ownedRef = ownedRef;
64 	}
65 
66 	~this ()
67 	{
68 		if (  Linker.isLoaded(LIBRARY_GLIB) && ownedRef )
69 			g_rand_free(gRand);
70 	}
71 
72 
73 	/**
74 	 * Copies a #GRand into a new one with the same exact state as before.
75 	 * This way you can take a snapshot of the random number generator for
76 	 * replaying later.
77 	 *
78 	 * Returns: the new #GRand
79 	 *
80 	 * Since: 2.4
81 	 */
82 	public RandG copy()
83 	{
84 		auto p = g_rand_copy(gRand);
85 		
86 		if(p is null)
87 		{
88 			return null;
89 		}
90 		
91 		return new RandG(cast(GRand*) p);
92 	}
93 
94 	/**
95 	 * Returns the next random #gdouble from @rand_ equally distributed over
96 	 * the range [0..1).
97 	 *
98 	 * Returns: a random number
99 	 */
100 	public double randDouble()
101 	{
102 		return g_rand_double(gRand);
103 	}
104 
105 	/**
106 	 * Returns the next random #gdouble from @rand_ equally distributed over
107 	 * the range [@begin..@end).
108 	 *
109 	 * Params:
110 	 *     begin = lower closed bound of the interval
111 	 *     end = upper open bound of the interval
112 	 *
113 	 * Returns: a random number
114 	 */
115 	public double doubleRange(double begin, double end)
116 	{
117 		return g_rand_double_range(gRand, begin, end);
118 	}
119 
120 	/**
121 	 * Frees the memory allocated for the #GRand.
122 	 */
123 	public void free()
124 	{
125 		g_rand_free(gRand);
126 		ownedRef = false;
127 	}
128 
129 	/**
130 	 * Returns the next random #guint32 from @rand_ equally distributed over
131 	 * the range [0..2^32-1].
132 	 *
133 	 * Returns: a random number
134 	 */
135 	public uint randInt()
136 	{
137 		return g_rand_int(gRand);
138 	}
139 
140 	/**
141 	 * Returns the next random #gint32 from @rand_ equally distributed over
142 	 * the range [@begin..@end-1].
143 	 *
144 	 * Params:
145 	 *     begin = lower closed bound of the interval
146 	 *     end = upper open bound of the interval
147 	 *
148 	 * Returns: a random number
149 	 */
150 	public int intRange(int begin, int end)
151 	{
152 		return g_rand_int_range(gRand, begin, end);
153 	}
154 
155 	/**
156 	 * Sets the seed for the random number generator #GRand to @seed.
157 	 *
158 	 * Params:
159 	 *     seed = a value to reinitialize the random number generator
160 	 */
161 	public void setSeed(uint seed)
162 	{
163 		g_rand_set_seed(gRand, seed);
164 	}
165 
166 	/**
167 	 * Initializes the random number generator by an array of longs.
168 	 * Array can be of arbitrary size, though only the first 624 values
169 	 * are taken.  This function is useful if you have many low entropy
170 	 * seeds, or if you require more then 32 bits of actual entropy for
171 	 * your application.
172 	 *
173 	 * Params:
174 	 *     seed = array to initialize with
175 	 *     seedLength = length of array
176 	 *
177 	 * Since: 2.4
178 	 */
179 	public void setSeedArray(uint* seed, uint seedLength)
180 	{
181 		g_rand_set_seed_array(gRand, seed, seedLength);
182 	}
183 
184 	/**
185 	 * Creates a new random number generator initialized with a seed taken
186 	 * either from `/dev/urandom` (if existing) or from the current time
187 	 * (as a fallback).
188 	 *
189 	 * On Windows, the seed is taken from rand_s().
190 	 *
191 	 * Returns: the new #GRand
192 	 *
193 	 * Throws: ConstructionException GTK+ fails to create the object.
194 	 */
195 	public this()
196 	{
197 		auto p = g_rand_new();
198 		
199 		if(p is null)
200 		{
201 			throw new ConstructionException("null returned by new");
202 		}
203 		
204 		this(cast(GRand*) p);
205 	}
206 
207 	/**
208 	 * Creates a new random number generator initialized with @seed.
209 	 *
210 	 * Params:
211 	 *     seed = a value to initialize the random number generator
212 	 *
213 	 * Returns: the new #GRand
214 	 *
215 	 * Throws: ConstructionException GTK+ fails to create the object.
216 	 */
217 	public this(uint seed)
218 	{
219 		auto p = g_rand_new_with_seed(seed);
220 		
221 		if(p is null)
222 		{
223 			throw new ConstructionException("null returned by new_with_seed");
224 		}
225 		
226 		this(cast(GRand*) p);
227 	}
228 
229 	/**
230 	 * Creates a new random number generator initialized with @seed.
231 	 *
232 	 * Params:
233 	 *     seed = an array of seeds to initialize the random number generator
234 	 *     seedLength = an array of seeds to initialize the random number
235 	 *         generator
236 	 *
237 	 * Returns: the new #GRand
238 	 *
239 	 * Since: 2.4
240 	 *
241 	 * Throws: ConstructionException GTK+ fails to create the object.
242 	 */
243 	public this(uint[] seed)
244 	{
245 		auto p = g_rand_new_with_seed_array(seed.ptr, cast(uint)seed.length);
246 		
247 		if(p is null)
248 		{
249 			throw new ConstructionException("null returned by new_with_seed_array");
250 		}
251 		
252 		this(cast(GRand*) p);
253 	}
254 
255 	/**
256 	 * Returns a random #gdouble equally distributed over the range [0..1).
257 	 *
258 	 * Returns: a random number
259 	 */
260 	public static double randomDouble()
261 	{
262 		return g_random_double();
263 	}
264 
265 	/**
266 	 * Returns a random #gdouble equally distributed over the range
267 	 * [@begin..@end).
268 	 *
269 	 * Params:
270 	 *     begin = lower closed bound of the interval
271 	 *     end = upper open bound of the interval
272 	 *
273 	 * Returns: a random number
274 	 */
275 	public static double randomDoubleRange(double begin, double end)
276 	{
277 		return g_random_double_range(begin, end);
278 	}
279 
280 	/**
281 	 * Return a random #guint32 equally distributed over the range
282 	 * [0..2^32-1].
283 	 *
284 	 * Returns: a random number
285 	 */
286 	public static uint randomInt()
287 	{
288 		return g_random_int();
289 	}
290 
291 	/**
292 	 * Returns a random #gint32 equally distributed over the range
293 	 * [@begin..@end-1].
294 	 *
295 	 * Params:
296 	 *     begin = lower closed bound of the interval
297 	 *     end = upper open bound of the interval
298 	 *
299 	 * Returns: a random number
300 	 */
301 	public static int randomIntRange(int begin, int end)
302 	{
303 		return g_random_int_range(begin, end);
304 	}
305 
306 	/**
307 	 * Sets the seed for the global random number generator, which is used
308 	 * by the g_random_* functions, to @seed.
309 	 *
310 	 * Params:
311 	 *     seed = a value to reinitialize the global random number generator
312 	 */
313 	public static void randomSetSeed(uint seed)
314 	{
315 		g_random_set_seed(seed);
316 	}
317 }