How to generate random numbers in C++ of more than 7 digits?

Se how to use function srand() and rand() to generate random numbers, the problem is that those numbers are limited to 5 digits at most and I need to generate 7 or 8 digit numbers. Reading a bit I find that RAND_MAX has the maximum value I can generate and when printing it to console it is 32767. In other sources they say that the value of RAND_MAX should be billions so I do not know what to believe.

Any idea how to generate more numbers big?

I would appreciate it very much.

 6
Author: PaperBirdMaster, 2017-03-05

1 answers

In your case we run into two problems.

Limitations of rand.

The function std::rand (and by extension, all related as std::srand they are based on the pseudo-random number generation engine of C. In fact both functions are a C function adapted to C++.

The return type of std::rand is a int, so the range of numbers it can return belong to the range of int, whose range depends on the platform :

  • in 16-bit systems, the number of numbers storable per int is 216:
    • 65,535 values.
  • in 32-bit systems, the number of numbers storable per int is 232:
    • 4,294,967,295 values.
  • in 64-bit systems, the number of numbers storable per int is 264:
    • 18.446.744.073.709.551.615 stock.

As you can see, unless the integer is 16 bits you can get 7-digit numbers without problems.

Reading a bit I find that RAND_MAX has the maximum value I can generate and when printing it to console it is 32767.

That is the maximum positive value of the signed integer on a 16-bit platform. Since it is signed, half of the values are reserved for negative numbers and the other for positive.

So, apparently you're working on a 16-bit platform.

Proposals.

C++11

Uses the pseudo-random number header of C++11 <random> (not that of C, part of <cstdlib>):

#include <random>
#include <iostream>

int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());
    // Siempre 7 cifras
    std::uniform_int_distribution<> dis(1000000, 9999999);

    for (int n = 0; n < 10; ++n)
        std::cout << dis(gen) << ' ';
    std::cout << '\n';
}

You can see the code working [here] .

uses a string / array of characters

If your 16-bit compiler doesn't support C++11 it's going to be tricky to get numbers from more than 5 digits, but you can cheat and use strings:

#include <iostream>
#include <cstdlib>

void numero_de_7_cifras(char (&numero)[8])
{
    for (int indice = 0; indice != 7; ++indice)
    {
        numero[indice] = '0' + std::rand() % 10;
    }
}

int main()
{
    char numero[8] = {};

    for (int n = 0; n < 10; ++n)
    {
        numero_de_7_cifras(numero);
        std::cout << numero << ' ';
    }
    std::cout << '\n';
}

You can see the code working [here] .

The problem with this approximation is that some digits have a slight chance of appearing more often and that sometimes the number can start with 0, but I leave it to your discretion if that is indifferent to you.


I tried using the C++ library <random> but codeblocks doesn't recognize it.

To header <random> it belongs to the 2011 C++ standard, known as C++11. If your compiler does not support the C++11 standard (or higher), you will not have such a header.

Code:: Blocks is the IDE you're using, not the compiler. Normally the IDE installs a compiler and the installed compiler makes its libraries available to the user.

If your IDE is recent you should have installed a compiler with the C++11 libraries; you can if it is necessary to indicate that you want to use the C++11 standard in the parameters passed to the compiler, the exact parameter will depend on the compiler you are using(but it could be -std=c++11).

My system is 64-bit, [...] any idea why he behaves like he's 16?

I think my initial answer may have led to confusion. On the one hand the system can be 16, 32 or 64 bits, but on the other hand the compiler (if it can and is configured properly) you can compile for 16, 32 or 64-bit systems. If your RAND_MAX has a value of 32767 it may be because the compiler can only compile to 16 bits or because it is set to compile to 16 bits, I bet that's the latter.

 6
Author: PaperBirdMaster, 2017-03-07 08:18:27