* Returns a single random integer between zero and the given max value or the given range. * The bounds are inclusive. * * @param options Maximum value or options object. * @param options.min Lower bound for generated number. Defaults to `0`. * @param options.max Upper bound for gener
(
options:
| number
| {
/**
* Lower bound for generated number.
*
* @default 0
*/
min?: number;
/**
* Upper bound for generated number.
*
* @default Number.MAX_SAFE_INTEGER
*/
max?: number;
/**
* Generated number will be a multiple of the given integer.
*
* @default 1
*/
multipleOf?: number;
/**
* A function to determine the distribution of generated values.
*
* @default uniformDistributor()
*/
distributor?: Distributor;
} = {}
)
| 45 | * @since 8.0.0 |
| 46 | */ |
| 47 | int( |
| 48 | options: |
| 49 | | number |
| 50 | | { |
| 51 | /** |
| 52 | * Lower bound for generated number. |
| 53 | * |
| 54 | * @default 0 |
| 55 | */ |
| 56 | min?: number; |
| 57 | /** |
| 58 | * Upper bound for generated number. |
| 59 | * |
| 60 | * @default Number.MAX_SAFE_INTEGER |
| 61 | */ |
| 62 | max?: number; |
| 63 | /** |
| 64 | * Generated number will be a multiple of the given integer. |
| 65 | * |
| 66 | * @default 1 |
| 67 | */ |
| 68 | multipleOf?: number; |
| 69 | /** |
| 70 | * A function to determine the distribution of generated values. |
| 71 | * |
| 72 | * @default uniformDistributor() |
| 73 | */ |
| 74 | distributor?: Distributor; |
| 75 | } = {} |
| 76 | ): number { |
| 77 | if (typeof options === 'number') { |
| 78 | options = { max: options }; |
| 79 | } |
| 80 | |
| 81 | const { |
| 82 | min = 0, |
| 83 | max = Number.MAX_SAFE_INTEGER, |
| 84 | multipleOf = 1, |
| 85 | distributor = uniformDistributor(), |
| 86 | } = options; |
| 87 | |
| 88 | if (!Number.isInteger(multipleOf)) { |
| 89 | throw new FakerError(`multipleOf should be an integer.`); |
| 90 | } |
| 91 | |
| 92 | if (multipleOf <= 0) { |
| 93 | throw new FakerError(`multipleOf should be greater than 0.`); |
| 94 | } |
| 95 | |
| 96 | const effectiveMin = Math.ceil(min / multipleOf); |
| 97 | const effectiveMax = Math.floor(max / multipleOf); |
| 98 | |
| 99 | if (effectiveMin === effectiveMax) { |
| 100 | return effectiveMin * multipleOf; |
| 101 | } |
| 102 | |
| 103 | if (effectiveMax < effectiveMin) { |
| 104 | if (max >= min) { |