lundi 28 octobre 2019

C++ RNG (Random Number Generation) Optimization

First of all I'd like to clarify I'm quite new to C++, and I've put together some code that outputs a random set of 16 digits. For that I used a tutorial online and the comparatively higher batch experience (this idea came from a batch file that was running quite slowly and I wanted to migrate both batch files to c++ to compare how they ran, as there were two similar projects, one of them for random number generation, which is this one, and another for random string generation). As I'm not that great with c++, I'd like some help optimizing the code, as I feel it can be done in fewer lines Now that you've got the background, my code is as follows

#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
    std::srand(std::time(0));
    int random0 = 0;
    int random1 = 0;
    int random2 = 0;
    int random3 = 0;
    int random4 = 0;
    int random5 = 0;
    int random6 = 0;
    int random7 = 0;
    int random8 = 0;
    int random9 = 0;
    int random10 = 0;
    int random11 = 0;
    int random12 = 0;
    int random13 = 0;
    int random14 = 0;
    int random15 = 0;


    for (int i = 0; i < i+1; ++i)
    {
        random0 = std::rand() % 9;
        random1 = std::rand() % 9;
        random2 = std::rand() % 9;
        random3 = std::rand() % 9;
        random4 = std::rand() % 9;
        random5 = std::rand() % 9;
        random6 = std::rand() % 9;
        random7 = std::rand() % 9;
        random8 = std::rand() % 9;
        random9 = std::rand() % 9;
        random10 = std::rand() % 9;
        random11 = std::rand() % 9;
        random12 = std::rand() % 9;
        random13 = std::rand() % 9;
        random14 = std::rand() % 9;
        random15 = std::rand() % 9;

        std::cout << random0 << random1 << random2 << random3 << random4 << random5 << random6 << random7 << random8 << random9 << random10 << random11 << random12 << random13 << random14 << random15 << std::endl;
    }
    return 0;
}

Thanks in advance for any help, and I'm sorry if I trigger any experienced c++ coder's OCD.




Aucun commentaire:

Enregistrer un commentaire