版博士V2.0程序
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

46 líneas
1.4 KiB

  1. /**
  2. * @since 2.0.0
  3. */
  4. import { map } from './IO';
  5. import { pipe } from './function';
  6. /**
  7. * Returns a random number between 0 (inclusive) and 1 (exclusive). This is a direct wrapper around JavaScript's
  8. * `Math.random()`.
  9. *
  10. * @since 2.0.0
  11. */
  12. export var random = function () { return Math.random(); };
  13. /**
  14. * Takes a range specified by `low` (the first argument) and `high` (the second), and returns a random integer uniformly
  15. * distributed in the closed interval `[low, high]`. It is unspecified what happens if `low > high`, or if either of
  16. * `low` or `high` is not an integer.
  17. *
  18. * @since 2.0.0
  19. */
  20. export function randomInt(low, high) {
  21. return pipe(random, map(function (n) { return Math.floor((high - low + 1) * n + low); }));
  22. }
  23. /**
  24. * Returns a random number between a minimum value (inclusive) and a maximum value (exclusive). It is unspecified what
  25. * happens if `maximum < minimum`.
  26. *
  27. * @since 2.0.0
  28. */
  29. export function randomRange(min, max) {
  30. return pipe(random, map(function (n) { return (max - min) * n + min; }));
  31. }
  32. /**
  33. * Returns a random boolean value with an equal chance of being `true` or `false`
  34. *
  35. * @since 2.0.0
  36. */
  37. export var randomBool = /*#__PURE__*/ pipe(random, map(function (n) { return n < 0.5; }));
  38. /**
  39. * Returns a random element of a `ReadonlyNonEmptyArray`.
  40. *
  41. * @since 2.10.0
  42. */
  43. export var randomElem = function (as) {
  44. return pipe(randomInt(0, as.length - 1), map(function (i) { return as[i]; }));
  45. };