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 glib.c.functions;
29 public  import glib.c.types;
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 	 *
235 	 * Returns: the new #GRand
236 	 *
237 	 * Since: 2.4
238 	 *
239 	 * Throws: ConstructionException GTK+ fails to create the object.
240 	 */
241 	public this(uint[] seed)
242 	{
243 		auto __p = g_rand_new_with_seed_array(seed.ptr, cast(uint)seed.length);
244 
245 		if(__p is null)
246 		{
247 			throw new ConstructionException("null returned by new_with_seed_array");
248 		}
249 
250 		this(cast(GRand*) __p);
251 	}
252 
253 	/**
254 	 * Returns a random #gdouble equally distributed over the range [0..1).
255 	 *
256 	 * Returns: a random number
257 	 */
258 	public static double randomDouble()
259 	{
260 		return g_random_double();
261 	}
262 
263 	/**
264 	 * Returns a random #gdouble equally distributed over the range
265 	 * [@begin..@end).
266 	 *
267 	 * Params:
268 	 *     begin = lower closed bound of the interval
269 	 *     end = upper open bound of the interval
270 	 *
271 	 * Returns: a random number
272 	 */
273 	public static double randomDoubleRange(double begin, double end)
274 	{
275 		return g_random_double_range(begin, end);
276 	}
277 
278 	/**
279 	 * Return a random #guint32 equally distributed over the range
280 	 * [0..2^32-1].
281 	 *
282 	 * Returns: a random number
283 	 */
284 	public static uint randomInt()
285 	{
286 		return g_random_int();
287 	}
288 
289 	/**
290 	 * Returns a random #gint32 equally distributed over the range
291 	 * [@begin..@end-1].
292 	 *
293 	 * Params:
294 	 *     begin = lower closed bound of the interval
295 	 *     end = upper open bound of the interval
296 	 *
297 	 * Returns: a random number
298 	 */
299 	public static int randomIntRange(int begin, int end)
300 	{
301 		return g_random_int_range(begin, end);
302 	}
303 
304 	/**
305 	 * Sets the seed for the global random number generator, which is used
306 	 * by the g_random_* functions, to @seed.
307 	 *
308 	 * Params:
309 	 *     seed = a value to reinitialize the global random number generator
310 	 */
311 	public static void randomSetSeed(uint seed)
312 	{
313 		g_random_set_seed(seed);
314 	}
315 }