imneme / pcg-cpp

PCG — C++ Implementation
Apache License 2.0
745 stars 99 forks source link

computing 53-bits random number #55

Open ng5 opened 4 years ago

ng5 commented 4 years ago

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

2) 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

3) 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.