Skip to content

Instantly share code, notes, and snippets.

@erkobridee
Last active April 9, 2025 06:08
Show Gist options
  • Save erkobridee/390464dfcbe88968405e130556c10d8d to your computer and use it in GitHub Desktop.
Save erkobridee/390464dfcbe88968405e130556c10d8d to your computer and use it in GitHub Desktop.
/*
useful references:
https://github.com/erkobridee/lab-pdf-lib/blob/83ef7e53e61227cef4293a6294979b1e551a3603/src/helpers/random.js#L4
https://github.com/erkobridee/nx-todos/blob/1647f66518a6def9ddf7569ca7a2b6397337d09c/libs/shared/helpers/src/lib/values.ts#L25
https://github.com/erkobridee/gatsby-typescript-app-starter/blob/5a7bcfbbe6ff014ecb360da35a3e8255fe80bf9e/src/helpers/values.ts#L21
https://gist.github.com/rubinchyk/46d9868e2eb86d744abcb83b5b6d8d29
https://gist.github.com/erkobridee/ac14ce7f52f66a05747511adf33bc2fa
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
*/
const getRandomBoolean = () => Math.random() >= 0.5;
const getRandomFloat = (min, max) => Math.random() * (max - min + 1) + min;
const getRandomDecimal = (min, max, fixed = 2) => +getRandomFloat(min, max).toFixed(fixed);
const getRandomInt = (min, max) => Math.floor(getRandomFloat(min, max));
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
const getRandomIntAsString = (min, max, strLength = String(max).length) => String(getRandomInt(min, max)).padStart(strLength, '0');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment