random number above 32767

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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
	if (Maxdigit == 5)//  maximum digit of the desire random #
	{
		mNumber = 100000;
	}
	for (int i=0; i<numbTotal; i++)// total of random number desire
	{
		randomNumber = rand()%mNumber;
		if (Maxdigit == 5 && randomNumber >= 10000)
		{
			cout << randomNumber << "\t";
		}
	}

	if (Maxdigit == 6)
	{
		mNumber = 10000000;
	}
	for (int i=0; i<numbTotal; i++)
	{
		randomNumber = rand()%mNumber;
		if (Maxdigit == 6 && randomNumber >= 10 * 100000)
		{
			cout << randomNumber << "\t";
		}
	}
There are good examples here: http://www.cplusplus.com/reference/cstdlib/rand/

e.g. with Maxdigit == 5: rand()%(100000-32767)+32767
Use the C++11 random number library.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <random>
#include <ctime>
#include <iostream>

int main()
{
    const int max_digits = 12 ; // max digits in the random number generated

    // compute max value
    long long 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<long long> distr( -max_value, max_value ) ;

    for( int i = 0 ; i < 20 ; ++i )
    {
        const long long random_number = distr(rng) ; // generate a random number
        std::cout << random_number << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/f94e980761140dbc
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.
thanks
Topic archived. No new replies allowed.