1 | import baseRandom from './_baseRandom.js';
|
---|
2 | import isIterateeCall from './_isIterateeCall.js';
|
---|
3 | import toFinite from './toFinite.js';
|
---|
4 |
|
---|
5 | /** Built-in method references without a dependency on `root`. */
|
---|
6 | var freeParseFloat = parseFloat;
|
---|
7 |
|
---|
8 | /* Built-in method references for those with the same name as other `lodash` methods. */
|
---|
9 | var 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 | */
|
---|
43 | function 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 |
|
---|
82 | export default random;
|
---|