Skip to content

Instantly share code, notes, and snippets.

@nikoloza
Last active October 10, 2016 20:20
Show Gist options
  • Save nikoloza/7772b1324fe254de6eab77da8ec722e7 to your computer and use it in GitHub Desktop.
Save nikoloza/7772b1324fe254de6eab77da8ec722e7 to your computer and use it in GitHub Desktop.
I've heard Math.random() is based on timestamp. I wanted to check how we can predict it with maximum accuracy. So if that two numbers are close together (can be the same in multithreads maybe), we can definitely check and hack any system based on Math.random().
(function(interval){
const start = Date.now()
const end = start + 1000
let i = 0
function hackRandom () {
let now = Date.now()
if (now > end) {
console.log(`timestamp: ${now}`, `random: ${Math.random()}`)
i++
}
if (i > 1) {
clearInterval(a)
clearInterval(b)
}
}
var a = setInterval(hackRandom, interval)
var b = setInterval(hackRandom, interval)
})(100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment