Reputation: 155
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?
Upvotes: 8
Views: 19380
Reputation: 181
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.
Upvotes: 0
Reputation: 51
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.
Upvotes: 1
Reputation: 294247
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.
Upvotes: 2
Reputation: 1626
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.
Upvotes: -2
Reputation: 50053
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.
Upvotes: 6
Reputation: 9705
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;
}
Upvotes: 22