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 private import gtkc.Loader; 68 private import gtkc.paths; 69 70 71 72 /** 73 * The following functions allow you to use a portable, fast and good 74 * pseudo-random number generator (PRNG). It uses the Mersenne Twister 75 * PRNG, which was originally developed by Makoto Matsumoto and Takuji 76 * Nishimura. Further information can be found at 77 * 78 * http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html. 79 * 80 * If you just need a random number, you simply call the 81 * g_random_* functions, which will create a 82 * globally used GRand and use the according 83 * g_rand_* functions internally. Whenever you 84 * need a stream of reproducible random numbers, you better create a 85 * GRand yourself and use the g_rand_* functions 86 * directly, which will also be slightly faster. Initializing a GRand 87 * with a certain seed will produce exactly the same series of random 88 * numbers on all platforms. This can thus be used as a seed for e.g. 89 * games. 90 * 91 * The g_rand*_range functions will return high 92 * quality equally distributed random numbers, whereas for example the 93 * (g_random_int()%max) approach often 94 * doesn't yield equally distributed numbers. 95 * 96 * GLib changed the seeding algorithm for the pseudo-random number 97 * generator Mersenne Twister, as used by 98 * GRand and GRandom. 99 * This was necessary, because some seeds would yield very bad 100 * pseudo-random streams. Also the pseudo-random integers generated by 101 * g_rand*_int_range() will have a slightly better 102 * equal distribution with the new version of GLib. 103 * 104 * The original seeding and generation algorithms, as found in GLib 105 * 2.0.x, can be used instead of the new ones by setting the 106 * environment variable G_RANDOM_VERSION to the value of 107 * '2.0'. Use the GLib-2.0 algorithms only if you have sequences of 108 * numbers generated with Glib-2.0 that you need to reproduce exactly. 109 */ 110 public class RandG 111 { 112 113 /** the main Gtk struct */ 114 protected GRand* gRand; 115 116 117 /** Get the main Gtk struct */ 118 public GRand* getRandGStruct() 119 { 120 return gRand; 121 } 122 123 124 /** the main Gtk struct as a void* */ 125 protected void* getStruct() 126 { 127 return cast(void*)gRand; 128 } 129 130 /** 131 * Sets our main struct and passes it to the parent class 132 */ 133 public this (GRand* gRand) 134 { 135 this.gRand = gRand; 136 } 137 138 ~this () 139 { 140 if ( Linker.isLoaded(LIBRARY.GLIB) && gRand !is null ) 141 { 142 g_rand_free(gRand); 143 } 144 } 145 146 /** 147 */ 148 149 /** 150 * Creates a new random number generator initialized with seed. 151 * Params: 152 * seed = a value to initialize the random number generator. 153 * Throws: ConstructionException GTK+ fails to create the object. 154 */ 155 public this (uint seed) 156 { 157 // GRand * g_rand_new_with_seed (guint32 seed); 158 auto p = g_rand_new_with_seed(seed); 159 if(p is null) 160 { 161 throw new ConstructionException("null returned by g_rand_new_with_seed(seed)"); 162 } 163 this(cast(GRand*) p); 164 } 165 166 /** 167 * Creates a new random number generator initialized with seed. 168 * Since 2.4 169 * Params: 170 * seed = an array of seeds to initialize the random number generator. 171 * Throws: ConstructionException GTK+ fails to create the object. 172 */ 173 public this (uint[] seed) 174 { 175 // GRand * g_rand_new_with_seed_array (const guint32 *seed, guint seed_length); 176 auto p = g_rand_new_with_seed_array(seed.ptr, cast(int) seed.length); 177 if(p is null) 178 { 179 throw new ConstructionException("null returned by g_rand_new_with_seed_array(seed.ptr, cast(int) seed.length)"); 180 } 181 this(cast(GRand*) p); 182 } 183 184 /** 185 * Creates a new random number generator initialized with a seed taken 186 * either from /dev/urandom (if existing) or from 187 * the current time (as a fallback). 188 * Throws: ConstructionException GTK+ fails to create the object. 189 */ 190 public this () 191 { 192 // GRand * g_rand_new (void); 193 auto p = g_rand_new(); 194 if(p is null) 195 { 196 throw new ConstructionException("null returned by g_rand_new()"); 197 } 198 this(cast(GRand*) p); 199 } 200 201 /** 202 * Copies a GRand into a new one with the same exact state as before. 203 * This way you can take a snapshot of the random number generator for 204 * replaying later. 205 * Since 2.4 206 * Returns: the new GRand. 207 */ 208 public RandG copy() 209 { 210 // GRand * g_rand_copy (GRand *rand_); 211 auto p = g_rand_copy(gRand); 212 213 if(p is null) 214 { 215 return null; 216 } 217 218 return new RandG(cast(GRand*) p); 219 } 220 221 /** 222 * Frees the memory allocated for the GRand. 223 */ 224 public void free() 225 { 226 // void g_rand_free (GRand *rand_); 227 g_rand_free(gRand); 228 } 229 230 /** 231 * Sets the seed for the random number generator GRand to seed. 232 * Params: 233 * seed = a value to reinitialize the random number generator. 234 */ 235 public void setSeed(uint seed) 236 { 237 // void g_rand_set_seed (GRand *rand_, guint32 seed); 238 g_rand_set_seed(gRand, seed); 239 } 240 241 /** 242 * Initializes the random number generator by an array of 243 * longs. Array can be of arbitrary size, though only the 244 * first 624 values are taken. This function is useful 245 * if you have many low entropy seeds, or if you require more then 246 * 32bits of actual entropy for your application. 247 * Since 2.4 248 * Params: 249 * seed = array to initialize with 250 */ 251 public void setSeedArray(uint[] seed) 252 { 253 // void g_rand_set_seed_array (GRand *rand_, const guint32 *seed, guint seed_length); 254 g_rand_set_seed_array(gRand, seed.ptr, cast(int) seed.length); 255 } 256 257 /** 258 * Returns the next random guint32 from rand_ equally distributed over 259 * the range [0..2^32-1]. 260 * Params: 261 * rand = a GRand. 262 * Returns: A random number. 263 */ 264 public uint randInt() 265 { 266 // guint32 g_rand_int (GRand *rand_); 267 return g_rand_int(gRand); 268 } 269 270 /** 271 * Returns the next random gint32 from rand_ equally distributed over 272 * the range [begin..end-1]. 273 * Params: 274 * rand = a GRand. 275 * begin = lower closed bound of the interval. 276 * end = upper open bound of the interval. 277 * Returns: A random number. 278 */ 279 public int randIntRange(int begin, int end) 280 { 281 // gint32 g_rand_int_range (GRand *rand_, gint32 begin, gint32 end); 282 return g_rand_int_range(gRand, begin, end); 283 } 284 285 /** 286 * Returns the next random gdouble from rand_ equally distributed over 287 * the range [0..1). 288 * Params: 289 * rand = a GRand. 290 * Returns: A random number. 291 */ 292 public double randDouble() 293 { 294 // gdouble g_rand_double (GRand *rand_); 295 return g_rand_double(gRand); 296 } 297 298 /** 299 * Returns the next random gdouble from rand_ equally distributed over 300 * the range [begin..end). 301 * Params: 302 * rand = a GRand. 303 * begin = lower closed bound of the interval. 304 * end = upper open bound of the interval. 305 * Returns: A random number. 306 */ 307 public double randDoubleRange(double begin, double end) 308 { 309 // gdouble g_rand_double_range (GRand *rand_, gdouble begin, gdouble end); 310 return g_rand_double_range(gRand, begin, end); 311 } 312 313 /** 314 * Sets the seed for the global random number generator, which is used 315 * by the g_random_* functions, to seed. 316 * Params: 317 * seed = a value to reinitialize the global random number generator. 318 */ 319 public static void randomSetSeed(uint seed) 320 { 321 // void g_random_set_seed (guint32 seed); 322 g_random_set_seed(seed); 323 } 324 325 /** 326 * Return a random guint32 equally distributed over the range 327 * [0..2^32-1]. 328 * Returns: A random number. 329 */ 330 public static uint randomInt() 331 { 332 // guint32 g_random_int (void); 333 return g_random_int(); 334 } 335 336 /** 337 * Returns a random gint32 equally distributed over the range 338 * [begin..end-1]. 339 * Params: 340 * begin = lower closed bound of the interval. 341 * end = upper open bound of the interval. 342 * Returns: A random number. 343 */ 344 public static int randomIntRange(int begin, int end) 345 { 346 // gint32 g_random_int_range (gint32 begin, gint32 end); 347 return g_random_int_range(begin, end); 348 } 349 350 /** 351 * Returns a random gdouble equally distributed over the range [0..1). 352 * Returns: A random number. 353 */ 354 public static double randomDouble() 355 { 356 // gdouble g_random_double (void); 357 return g_random_double(); 358 } 359 360 /** 361 * Returns a random gdouble equally distributed over the range [begin..end). 362 * Params: 363 * begin = lower closed bound of the interval. 364 * end = upper open bound of the interval. 365 * Returns: A random number. 366 */ 367 public static double randomDoubleRange(double begin, double end) 368 { 369 // gdouble g_random_double_range (gdouble begin, gdouble end); 370 return g_random_double_range(begin, end); 371 } 372 }