3 Pseudo-random generators.
5 mout uses `Math.random` by default on all the pseudo-random generators, if
6 you need a seeded random or a better algorithm see the [`random()`](#random)
7 documentation for instructions.
13 Returns a random element from the supplied arguments or from an array if single
19 choice(1, 2, 3, 4, 5); // 3
21 var arr = ['lorem', 'ipsum', 'dolor'];
22 choice(arr); // 'dolor'
29 Generates a pseudo-random [Globally Unique Identifier](http://en.wikipedia.org/wiki/Globally_unique_identifier) (v4).
31 Since the total number of GUIDs is 2<sup>122</sup> the chance of generating the
32 same value twice is negligible.
34 **Important:** this method uses `Math.random` by default so the UUID isn't
35 *safe* (sequence of outputs can be predicted in some cases), check the
36 [`random()`](#random) documentation for more info on how to replace the default
37 PRNG if you need extra safety or need *seeded* results.
39 See: [`randHex()`](#randHex), [`random()`](#random)
44 guid(); // 830e9f50-ac7f-4369-a14f-ed0e62b2fa0b
45 guid(); // 5de3d09b-e79c-4727-932b-48c49228d508
50 ## rand([min], [max]):Number
52 Gets a random number inside range or snap to min/max values.
56 1. `[min]` (Number) : Minimum value. Defaults to `number/MIN_INT`.
57 2. `[max]` (Number) : Maximum value. Defaults to `number/MAX_INT`.
63 rand(); // 448740433.55274725
64 rand(); // -31797596.097682
65 rand(0, 10); // 7.369723
66 rand(0, 10); // 5.987042
69 See: [`random()`](#random)
75 Returns a random "bit" (0 or 1). Useful for addition/subtraction.
77 It's slightly faster than `choice(0, 1)` since implementation is simpler (not
78 that it will make a huge difference in most cases).
80 See: [`choice()`](#choice)
95 Returns a random Boolean (`true` or `false`).
97 Since this is very common it makes sense to abstract it into a discrete method.
108 ## randHex([size]):String
110 Returns a random hexadecimal string.
112 The default `size` is `6`.
117 randHex(); // "dd8575"
118 randHex(); // "e6baeb"
120 randHex(30); // "effd7e2ad9a4a3067e30525fab983a"
125 ## randInt([min], [max]):Number
127 Gets a random integer inside range or snap to min/max values.
131 1. `[min]` (Number) : Minimum value. Defaults to `number/MIN_INT`.
132 2. `[max]` (Number) : Maximum value. Defaults to `number/MAX_INT`.
138 randInt(); // 448740433
139 randInt(); // -31797596
148 Returns a random "sign" (-1 or 1). Useful for multiplications.
150 It's slightly faster than `choice(-1, 1)` since implementation is simpler (not
151 that it will make a huge difference in most cases).
153 See: [`choice()`](#choice)
169 Returns a random number between `0` and `1`. Same as `Math.random()`.
172 random(); // 0.35435103671625257
173 random(); // 0.8768321881070733
176 **Important:** No methods inside mout should call `Math.random()`
177 directly, they all use `random/random` as a proxy, that way we can
178 inject/replace the pseudo-random number generator if needed (ie. in case we
179 need a seeded random or a better algorithm than the native one).
181 ### Replacing the PRNG
183 In some cases we might need better/different algorithms than the one provided
184 by `Math.random` (ie. safer, seeded).
186 Because of licensing issues, file size limitations and different needs we
187 decided to **not** implement a custom PRNG and instead provide a easy way to
188 override the default behavior. - [issue #99](https://github.com/millermedeiros/amd-utils/issues/99)
190 If you are using mout with a loader that supports the [AMD map
191 config](https://github.com/amdjs/amdjs-api/wiki/Common-Config), such as
192 [RequireJS](http://requirejs.org/), you can use it to replace the PRNG
193 (recommended approach):
198 // all modules will load "my_custom_prng" instead of
199 // "mout/random/random"
201 'mout/random/random' : 'my_custom_prng'
207 You also have the option to override `random.get` in case you are using
208 mout on node.js or with a loader which doesn't support the map config:
213 random.get = function(){
214 return ++n % 2? 0 : 1; // not so random :P
222 See this [detailed explanation about PRNG in
223 JavaScript](http://baagoe.org/en/w/index.php/Better_random_numbers_for_javascript)
224 to understand the issues with the native `Math.random` and also for a list of
225 algorithms that could be used instead.
229 ## randString([length, dictionary]):String
231 Returns a random string.
233 By default returns string containing alphanumeric characters (lowercase and uppercase) with a length of 8.
237 1. `[length]` (number) : Length of the string to return. Defaults to 8.
238 2. `[dictionary]` (string) : A string containing all characters used as a dictionary for the random string construction. Defaults to alphanumeric characters (lowercase and uppercase).
243 randString(); // returns a string with length 8.
244 randString(12); // returns a string of length 12.
245 randString(-1); // returns a string of length 8.
246 randString(null, 'pew!'); // returns a random string of length 8 composed of 'p', 'e', 'w' and '!'.
247 randString(10, '0'); // always returns '0's of length 10.
248 randString(rand(8, 10)); // returns a random string with length between 8 and 10.
253 -------------------------------------------------------------------------------
255 For more usage examples check specs inside `/tests` folder. Unit tests are the
256 best documentation you can get...