Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I randomly generate integers in interval <-99,99> in c++?

I tried something like this but this generate only in interval (-99,0)

void input(int array [row][col]){
    for (int i = 0; i < row; i++){
        for (int j = 0; j < col; j++){
            array[i][j] = rand() % 99 + (-99);
        }
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If you want to use the "old-fashioned" rand() instead of std::uniform_int_distribution, you could also do this:

for (int i = 0; i < row; i++){
    for (int j = 0; j < col; j++){
        int rnd_val = rand() % 199 // This generates a random value 
                                   // in the interval [0, 198].

        array[i][j] = rnd_val - 99; // This shifts the interval to [-99,99].
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading