2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-06-27 02:18:22 -04:00
|
|
|
|
|
|
|
// TODO(petamoriken): enable prefer-primordials for node polyfills
|
|
|
|
// deno-lint-ignore-file prefer-primordials
|
|
|
|
|
2024-01-29 08:58:08 -05:00
|
|
|
import { op_node_random_int } from "ext:core/ops";
|
2023-04-11 20:57:57 -04:00
|
|
|
|
2023-02-14 11:38:45 -05:00
|
|
|
export default function randomInt(max: number): number;
|
|
|
|
export default function randomInt(min: number, max: number): number;
|
|
|
|
export default function randomInt(
|
|
|
|
max: number,
|
|
|
|
cb: (err: Error | null, n?: number) => void,
|
|
|
|
): void;
|
|
|
|
export default function randomInt(
|
|
|
|
min: number,
|
|
|
|
max: number,
|
|
|
|
cb: (err: Error | null, n?: number) => void,
|
|
|
|
): void;
|
|
|
|
|
|
|
|
export default function randomInt(
|
|
|
|
max: number,
|
|
|
|
min?: ((err: Error | null, n?: number) => void) | number,
|
|
|
|
cb?: (err: Error | null, n?: number) => void,
|
|
|
|
): number | void {
|
|
|
|
if (typeof max === "number" && typeof min === "number") {
|
|
|
|
[max, min] = [min, max];
|
|
|
|
}
|
|
|
|
if (min === undefined) min = 0;
|
|
|
|
else if (typeof min === "function") {
|
|
|
|
cb = min;
|
|
|
|
min = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
!Number.isSafeInteger(min) ||
|
|
|
|
typeof max === "number" && !Number.isSafeInteger(max)
|
|
|
|
) {
|
|
|
|
throw new Error("max or min is not a Safe Number");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (max - min > Math.pow(2, 48)) {
|
|
|
|
throw new RangeError("max - min should be less than 2^48!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (min >= max) {
|
|
|
|
throw new Error("Min is bigger than Max!");
|
|
|
|
}
|
|
|
|
|
|
|
|
min = Math.ceil(min);
|
|
|
|
max = Math.floor(max);
|
2024-01-10 17:37:25 -05:00
|
|
|
const result = op_node_random_int(min, max);
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
if (cb) {
|
|
|
|
cb(null, result);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|