source: trip-planner-front/node_modules/lodash/random.js@ 76712b2

Last change on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.3 KB
Line 
1var baseRandom = require('./_baseRandom'),
2 isIterateeCall = require('./_isIterateeCall'),
3 toFinite = require('./toFinite');
4
5/** Built-in method references without a dependency on `root`. */
6var freeParseFloat = parseFloat;
7
8/* Built-in method references for those with the same name as other `lodash` methods. */
9var nativeMin = Math.min,
10 nativeRandom = Math.random;
11
12/**
13 * Produces a random number between the inclusive `lower` and `upper` bounds.
14 * If only one argument is provided a number between `0` and the given number
15 * is returned. If `floating` is `true`, or either `lower` or `upper` are
16 * floats, a floating-point number is returned instead of an integer.
17 *
18 * **Note:** JavaScript follows the IEEE-754 standard for resolving
19 * floating-point values which can produce unexpected results.
20 *
21 * @static
22 * @memberOf _
23 * @since 0.7.0
24 * @category Number
25 * @param {number} [lower=0] The lower bound.
26 * @param {number} [upper=1] The upper bound.
27 * @param {boolean} [floating] Specify returning a floating-point number.
28 * @returns {number} Returns the random number.
29 * @example
30 *
31 * _.random(0, 5);
32 * // => an integer between 0 and 5
33 *
34 * _.random(5);
35 * // => also an integer between 0 and 5
36 *
37 * _.random(5, true);
38 * // => a floating-point number between 0 and 5
39 *
40 * _.random(1.2, 5.2);
41 * // => a floating-point number between 1.2 and 5.2
42 */
43function random(lower, upper, floating) {
44 if (floating && typeof floating != 'boolean' && isIterateeCall(lower, upper, floating)) {
45 upper = floating = undefined;
46 }
47 if (floating === undefined) {
48 if (typeof upper == 'boolean') {
49 floating = upper;
50 upper = undefined;
51 }
52 else if (typeof lower == 'boolean') {
53 floating = lower;
54 lower = undefined;
55 }
56 }
57 if (lower === undefined && upper === undefined) {
58 lower = 0;
59 upper = 1;
60 }
61 else {
62 lower = toFinite(lower);
63 if (upper === undefined) {
64 upper = lower;
65 lower = 0;
66 } else {
67 upper = toFinite(upper);
68 }
69 }
70 if (lower > upper) {
71 var temp = lower;
72 lower = upper;
73 upper = temp;
74 }
75 if (floating || lower % 1 || upper % 1) {
76 var rand = nativeRandom();
77 return nativeMin(lower + (rand * (upper - lower + freeParseFloat('1e-' + ((rand + '').length - 1)))), upper);
78 }
79 return baseRandom(lower, upper);
80}
81
82module.exports = random;
Note: See TracBrowser for help on using the repository browser.