Skip to content

Instantly share code, notes, and snippets.

@luislobo14rap
Last active December 16, 2024 04:00
Show Gist options
  • Save luislobo14rap/de4b1628437a9d7ba95d233ada86405b to your computer and use it in GitHub Desktop.
Save luislobo14rap/de4b1628437a9d7ba95d233ada86405b to your computer and use it in GitHub Desktop.
random-between.js
// random-between.js v1.1
function randomBetween(min, max) {
const range = max + 1 - min
return min + Math.floor(Math.random() * range)
}
// random-between.js v2
function randomBetween(min, max) {
const range = max + 1 - min,
randomValue = (window.crypto || window.msCrypto).getRandomValues(new Uint32Array(1))[0]
return min + randomValue % range
}
// random-between.ts v1.1
function randomBetween(min: number, max: number): number {
const range = max + 1 - min
return min + Math.floor(Math.random() * range)
}
// random-boolean.js v1
function randomBoolean() {
return Boolean(randomBetween(0, 1));
};
@luislobo14rap
Copy link
Author

random date:

function randomBetween(min, max) {
const range = max + 1 - min
return min + Math.floor(Math.random() * range)
}

const random = randomBetween(
new Date("2020-01-01").getTime(),
new Date("2024-01-01").getTime()
)

console.log(new Date(random))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment