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

c++: Generate random numbers with seed

I am new to c++. I am trying to generate 4 random unique numbers from 0 to 9. Here is my code:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>  
#include <vector>
using namespace std;



int main()

{ 

vector<int> vect;
int randNums = 4;
int countSameNums = 0;
int randNumber; 

srand(time(0));  // Initialize random number generator by setting the seed
// generate 4 random unique integers and insert them all into a vector
while (true){
    randNumber = rand()%10;
    for (int i = 0; i < vect.size(); i++){
        if (randNumber == vect[i]){
            countSameNums += 1;
        }
    }
    if (countSameNums == 0){
        vect.push_back(randNumber);
        countSameNums = 0;
    }
    if (vect.size() == randNums)
        break;
}

for (int el : vect){ // print out the content of the vector
    cout << el <<endl;
}

}

I can compile the code without problems. However when I run it, sometimes it prints out the correct result, i.e. the 4 unique integers, but other times the program just hangs. I also notice that If I comment the line srand(time(0)) (my random seed) it prints 4 unique numbers every time, i.e. the program never hangs. The problem of course is that by commenting srand(time(0)) I get always the same sequence of unique numbers and I want a different sequence every run. Any idea on why this isn’t working? thanks

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

>Solution :

It can take long before you get 4 different numbers. There is a simpler and more efficient way to get 4 unique numbers. Take a vector containung number from 0 till 9, shuffle it, take the first 4.

Anyhow the issue in your code is that once countSameNum is not 0 you will never reset it to 0. You only reset it when it is 0 already. Change it like this:

while (true){
    randNumber = rand()%10;
    countSameNums = 0;
    for (int i = 0; i < vect.size(); i++){
        if (randNumber == vect[i]){
            countSameNums += 1;
        }
    }
    if (countSameNums == 0){
        vect.push_back(randNumber);
    }
    if (vect.size() == randNums)
        break;
}

As this is rather error-prone you should rather use std::find to see if the number is already in the vector:

if (std::find(vect.begin(),vect.end(),randNumber) == vect.end()) vect.push_back(randNumber);

And as mentioned before, there are better ways to get unique random numbers.

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