Last active
December 16, 2024 04:00
-
-
Save luislobo14rap/de4b1628437a9d7ba95d233ada86405b to your computer and use it in GitHub Desktop.
random-between.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// random-between.ts v1.1 | |
function randomBetween(min: number, max: number): number { | |
const range = max + 1 - min | |
return min + Math.floor(Math.random() * range) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// random-boolean.js v1 | |
function randomBoolean() { | |
return Boolean(randomBetween(0, 1)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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))