1

I wrote a simple random generator for my code and it's work perfectly. Only promblem is it do not create any number bigger than 5 digit.

int *numbers;

void Random()
{

    srand(time(NULL));

    cout << "Choose the amounth of the random values to create: ";
    cin >> amount;

    int max;

    cout << "Choose the max limit of the random numbers: ";
    cin >> max;

    numbers = new int[amount];

    //Create random
    for (int i = 0; i < amount; i++)
    {
        randomNumber = rand() % max;
        numbers[i] = randomNumber;
        cout << randomNumber << endl;
    }

}

When max is 1.000 (amount 10):
output: 12, 565, 404, 250, 745, 941, 814, 83, 277, 518

when max is 100.000 (amount 10):
output: 2592, 7665, 16438, 10927, 30746, 10054, 22957, 12322, 7029, 9814

when max is 10.000.000 (amount 10):
output: 2931, 11387, 6536, 23144, 22117, 25218, 18972, 2797, 20299, 25775

I thought maybe it was my luck why they are that small and i tryed multiple times. Even if i make the amount thousand nothing change, always small numbers.

Liakan
  • 165
  • 12
  • 1
    what is the value of the macro RAND_MAX ? – Pumkko Jan 12 '18 at 10:08
  • What type is randomNumber variable? It looks like it's short int. – ElChupacabra Jan 12 '18 at 10:08
  • 2
    Perhaps [this `std::rand` reference](http://en.cppreference.com/w/cpp/numeric/random/rand) might help? The value of [`RAND_MAX`](http://en.cppreference.com/w/cpp/numeric/random/RAND_MAX) (the maximum value returned by `rand`) is typically only `32767`, even on modern systems. If you want larger numbers [C++ have many other PRNG functionality](http://en.cppreference.com/w/cpp/numeric/random) you can use. – Some programmer dude Jan 12 '18 at 10:09
  • 2
    On an unrelated note, if you want an "array" whose size is set at run-time, use [`std::vector`](http://en.cppreference.com/w/cpp/container/vector) instead. – Some programmer dude Jan 12 '18 at 10:10

0 Answers0