generate random 64 bit integer

17,586

Solution 1

This could be a solution, without multiplication:

r30 = RAND_MAX*rand()+rand()
s30 = RAND_MAX*rand()+rand()
t4  = rand() & 0xf

res = (r30 << 34) + (s30 << 4) + t4

Solution 2

First, I have my doubts about the solution you post for a 30 bit integer. RAND_MAX itself could be a 31 bit value, and RAND_MAX * rand() + rand() is likely to overflow, producing undefined behavior (and in practice, negative values).

If you need a value larger than the guaranteed minimum of RAND_MAX, or for that matter, anything that isn't significantly smaller than RAND_MAX, the only solution will be to use successive calls to rand(), and combine the values, but you need to do this carefully, and validate the results. (Most implementations of rand() use linear congruent generators, which while adequate for some tasks, aren't particularly good in this case.) Anyway, something like:

unsigned 
rand256()
{
    static unsigned const limit = RAND_MAX - RAND_MAX % 256;
    unsigned result = rand();
    while ( result >= limit ) {
        result = rand();
    }
    return result % 256;
}

unsigned long long
rand64bits()
{
    unsigned long long results = 0ULL;
    for ( int count = 8; count > 0; -- count ) {
        results = 256U * results + rand256();
    }
    return results;
}

(The code in rand256 is designed to eliminate the otherwise unavoidable bias you get when mapping RAND_MAX values to 256 values.)

Solution 3

If boost is an option, you could use boost random.

Solution 4

A random 64 bit int is essentially 64 random bits interpreted as an int.

Fill a byte array of length 8 with random bytes (see here for how) and interpret these as an int (see here for how).

Solution 5

A generic solution:

template <unsigned long long I> struct log2 {
  static const int result = 1 + log2<I/2>::result;
};
template <> struct log2<1> {
  static const int result = 0;
};

template <typename UINT> UINT genrand() {
  UINT result = 0;
  int bits = std::numeric_limits<UINT>::digits;
  int rand_bits = log2<RAND_MAX>::result;
  while (bits > 0) {
    int r = rand();
    while (r >= (1<<rand_bits)) r = rand(); // Retry if too big.
    result <<= rand_bits;
    result += r;
    bits -= rand_bits;
  }
  return result;
}

Use: unsigned long long R = genrand<unsigned long long>();.

The bits counter keeps track of the number of bits still needed.

Share:
17,586
dato datuashvili
Author by

dato datuashvili

I am PHD student in international black sea university,city Tbilisi,capital of georgia,my interest field in my PHD degree is signal processing,additionaly calculus,linear algebra ,functional analysis,topology and combinatoric

Updated on June 04, 2022

Comments

  • dato datuashvili
    dato datuashvili almost 2 years

    I need your help and please give me some advice. From programming pearls I know that to generate random 30 bit integer we should write it like this:

    RAND_MAX*rand()+rand()
    

    But what could I do for generating not 30, but 64 bit random integer instead? I think that is very inefficient method if I multiply two 30 bit integers and then multiply again 4 bit integer, so what kind of method should I use? I am using now popcount_1 different method for 64 bit one and I would like to test it on random integers(I am also measuring the time which each one takes to accomplish the task)