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