0

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

diegus
  • 1,168
  • 2
  • 26
  • 57
  • Once `countSameNums` is not 0 you no longer add random numbers to the vector. – drescherjm Jul 01 '22 at 13:16
  • @463035818_is_not_a_number That seems a much better way of doing it. Thanks – diegus Jul 01 '22 at 13:16
  • 2
    An easy way to do this is add 0 to 9 to a vector and shuffle: [https://en.cppreference.com/w/cpp/algorithm/random_shuffle](https://en.cppreference.com/w/cpp/algorithm/random_shuffle) – drescherjm Jul 01 '22 at 13:18
  • Using % will stop each number being equally likely: https://stackoverflow.com/questions/10984974/why-do-people-say-there-is-modulo-bias-when-using-a-random-number-generator – doctorlove Jul 01 '22 at 13:20
  • The [random_shuffle](https://en.cppreference.com/w/cpp/algorithm/random_shuffle) also has an example which shows how to use C++ newer and improved random number generators, rather than the older and less randomy `rand` and its seed setter `srand`. – Eljay Jul 01 '22 at 14:27

1 Answers1

1

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.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185