Skip to content

Instantly share code, notes, and snippets.

@SamJakob
Created August 2, 2023 13:57

Revisions

  1. SamJakob created this gist Aug 2, 2023.
    24 changes: 24 additions & 0 deletions rand.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    const { promisify } = require('util');
    const exec = promisify(require('child_process').exec);

    /** Returns a random byte as a floating-point decimal between 0 and 1. */
    async function random() {
    const payload = await exec(`gpg-connect-agent "SCD RANDOM 1" /bye`);
    const randomBytes = payload.stdout.split('\n')[0].substr(2);

    const buffer = Buffer.from(randomBytes, 'utf-8');
    const number = buffer.readUIntBE(0, 1);
    return number;
    }

    (async () => {
    const lowerLimit = 1;
    const upperLimit = parseInt(process.argv[2], 10);

    let rand;
    do {
    rand = (await random()) + lowerLimit;
    } while (rand > upperLimit);

    console.log(rand);
    })();