Nabeel_Afzal Nabeel_Afzal - 3 months ago 10
C++ Question

How to generate very Large random number in c++

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?

Answer

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

Comments