I am using data from this site to get random numbers. They come in as a buffer, which I convert to binary, then into integer. I would like to finally convert those random numbers into decimal values between 0 and 1 like Math.random()
produces.
For example, if I have the integer 151
, what do I need to do to make it look more like this float 0.0151234252525372
.
Here is my code:
const bent = require('bent')
const getBuffer = bent('buffer')
let array = []
async function random(){
try{
let result = await getBuffer("https://qrng.anu.edu.au/wp-content/plugins/colours-plugin/get_one_binary.php")
let integer = parseInt(result.toString('utf8'), 2)
let float = parseFloat(integer) // convert to a decimal between 0 and 1 like Math.random() produces
array.push(float)
}
catch (error){return console.log(error)}
}
setInterval(()=>random().then(result => {
console.log(array)
}),50)
I'm not opposed to using the result of Math.random()
to apply some math to the initial random number, but I'm just not sure what the right math would be.
Aucun commentaire:
Enregistrer un commentaire