i'm trying to figure out how to get and display a random number above the 32767 integer. could some one give me a good explanation or some example to get a better understand it. Im using the following code to get random numbers but only work with less than 5 digits. any help?
#include <random>
#include <ctime>
#include <iostream>
int main()
{
constint max_digits = 12 ; // max digits in the random number generated
// compute max value
longlong max_value = 1 ;
for( int i = 0 ; i < max_digits ; ++i ) max_value *= 10 ;
max_value -= 1 ;
// random number generator seeded with time
// http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine
std::mt19937 rng( std::time(0) ) ;
// pseudo random integers uniformly distributed in [ -max_value, max_value ]
// http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution
std::uniform_int_distribution<longlong> distr( -max_value, max_value ) ;
for( int i = 0 ; i < 20 ; ++i )
{
constlonglong random_number = distr(rng) ; // generate a random number
std::cout << random_number << '\n' ;
}
}
Alternatively, if the library isn't available, rand()|(rand()<<15)|((rand()&3)<<30). Make sure that RAND_MAX == (1 << 16) - 1 before using this, though.