pcg-cpp icon indicating copy to clipboard operation
pcg-cpp copied to clipboard

computing 53-bits random number

Open ng5 opened this issue 4 years ago • 0 comments

I need to be able to send largest possible integer to a web application. This number will be passed back and forth to server.

template <typename T = pcg64_k1024_fast, typename R = int64_t> R computePCG() {
    static thread_local T rng{pcg_extras::seed_seq_from<std::random_device>()};
    return rng();
}

Using above code of PCG, On server I'm able to generate either 32-bit or 64-bit, However Json using 64 bit IEEE 754 - Only provides 53 bits precision for integer. So my options are:

  1. I can use full 64 bit integer, but this will require code change at client application to be able to handle 64 bit change. Would really like to avoid this approach.

OR

  1. Generate 64 bit number using PCG and right shift 11 bits ( To generate 53 bits), Is this safe approach to generate random numbers. Will this number be random enough atleast upto 53 bits.

OR

  1. Is there a better approach to specify number of byes required for random generation. Similar to javascript crypto function: https://nodejs.org/api/crypto.html#crypto_crypto_randombytes_size_callback

Thank you for your time and looking into it.

ng5 avatar Jan 19 '20 19:01 ng5