7

I want to generate very large random number in range of 0 - 2^64 using c++. I have used the rand() function but it is not generating very large number. Can any one help?

Nabeel_Afzal
  • 135
  • 1
  • 1
  • 11

6 Answers6

18

With c++11, using the standard random library of c++11, you can do this:

#include <iostream>
#include <random>

int main()
{
  /* Seed */
  std::random_device rd;

  /* Random number generator */
  std::default_random_engine generator(rd());

  /* Distribution on which to apply the generator */
  std::uniform_int_distribution<long long unsigned> distribution(0,0xFFFFFFFFFFFFFFFF);

  for (int i = 0; i < 10; i++) {
      std::cout << distribution(generator) << std::endl;
  }

  return 0;
}

Live Demo

πάντα ῥεῖ
  • 83,259
  • 13
  • 96
  • 175
coyotte508
  • 6,974
  • 5
  • 34
  • 58
  • 1
    Have never seen the word `API` being used for this. – Hatted Rooster May 23 '16 at 16:56
  • This solution is only generating large numbers . What if I want to generate the numbers in range of 0 - 2^64 ? – Nabeel_Afzal May 24 '16 at 18:08
  • 3
    @Nabeel_Afzal this solution does generate numbers in the range [0, 2^64-1]. But since the numbers are purely random and chosen uniformly in this interval, 99% of the numbers generated will be over 2^56 as [2^56, 2^64] is over 100 times bigger than [0, 2^56]. – coyotte508 May 24 '16 at 21:50
6

As a uniformly random number in the range [0, 2^64) is just 64 random bits, you can just use the return values of std::mt19937_64 directly:

#include <random>

int main () {
    std::mt19937_64 gen (std::random_device{}());

    std::uint64_t randomNumber = gen();
}

Note that seeding a Mersenne Twister engine with a single 32 bit seed is not optimal, for a better way, have a look at this.

Also note that the use of rand is generally discouraged these days. Here is a talk by Stephan T. Lavavej on that topic.

Baum mit Augen
  • 46,177
  • 22
  • 136
  • 173
1

I would also consider using the OS facilities instead. All modern systems have cryptographic modules capable of generating very good random byte arrays of arbitrary length. Linux has getrandom(). Windows has CryptGenRandom. OpenBSD has arc4random. iOS has SecRandomCopyBytes. etc. etc.

Remus Rusanu
  • 273,340
  • 38
  • 408
  • 539
1

I'wrote function that generates random 19 digit number It warks exactly like standard rand() function. It draws every digit from 19 digit number and stores them in an array, then it puts them together to make very big large random number.

unsigned long long Randomize()
{
    unsigned long long randnumber = 0;
    int digits[20];

    for (int i = 19; i >= 1; i--)
    {
      digits[i]=rand()%10:
    }
    for(int i=19; i>=1; i--)
    {
       unsigned long long power = pow(10, i-1);

        if (power%2 != 0 && power != 1)     //eliminates "bug" (which comes from long long power is not a float))
            power++;

        randnumber += power * digits[i];
    }
return randnumber;
}

To use this function you need to implement few libraries

#include <stdlib.h>
#include <time.h>
#include <math.h>

example:

srand(time(NULL));
randomnumber = Randomize()%10000000+10000000;

in this case random number is a number from 10000000 to 20000000.

Fiszu77
  • 51
  • 2
0

Late to answer the thread, but here is a simple approach

We know that rand() in c++ can generate a random number nearly up to 32767 (source: https://www.geeksforgeeks.org/rand-and-srand-in-ccpp/) But if we do something like this:

long long random_num = (rand() * rand()) % 1000000000

Then it is guaranteed to generate a much larger random number than 32767. The modulus will determine the number of digits in the random number.

-2

If your rand() function only gives numbers in the range [0, 2^15), then you can concatenate 5 numbers returned by rand() to get a number in the range [0, 2^64).

Of course, there are other possible solutions (which may be even better). The rand() function in C++ library is usually a linear congruential generator. You may simply implement your own generator, using the same mathematical principles.

For example, the following code generates 64 bit random numbers:

unsigned long long rand64()
{
    static unsigned long long seed;
    seed = seed * 6364136223846793005 + 1442695040888963407;
    return seed;
}

The parameters 6364136223846793005 and 1442695040888963407 are those used by Donald Knuth.

The advantages and disadvantages of this method is discussed in the above wiki page. If high quality randomness is not needed, it could be a good choice.

WhatsUp
  • 1,554
  • 9
  • 19
  • 3
    Why the XOR actually? And why sections of 20 bits? Would you elaborate on these magical numbers please? – πάντα ῥεῖ May 23 '16 at 16:50
  • 2
    Could you please explain the logic behind the return statement? – Nidhin David May 23 '16 at 16:50
  • 32768 is only 2^15, so doing these shifts by 20 won't fill all the bits. – interjay May 23 '16 at 17:00
  • 1
    Don't use `rand()` it generates *very* poor random numbers. Use the facilities in `` instead. And for very large numbers (larger than 2^64) maybe use the functions in the GMP library. – Jesper Juhl May 23 '16 at 17:01
  • So many casts (C style ones even). Pointless XOR operations (at least I don't see the point). Eeek. – Jesper Juhl May 23 '16 at 17:10
  • @JesperJuhl The casts and XORs are both necessary. But the amount of shifts is incorrect, as is using a signed type. – interjay May 23 '16 at 17:12
  • @interjay my point is simply that this is really ugly and one should just use the stuff from `` instead and the code will be clearer and the number quality better. This answer as written just makes me go 'eeew' - that looks like bad C. – Jesper Juhl May 23 '16 at 17:16