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