How do i avoid repeats of numbers when obtaining random numbers?

I made this vector filled with randomly generated numbers. I do not want repeats. is there any way I could do this without using random shuffle?

using namespace std;

int main()
{
int givenvalue = 10;
srand((unsigned)time(NULL));

vector<int> secretcode;
for (int i = 0; i < givenvalue; i++){
int b = rand() % 9 + 1;
secretcode.push_back(b);
cout << secretcode[i] << endl;
}
}
You can use std::unique_copy and copy them into a second vector.
http://www.cplusplus.com/reference/algorithm/unique_copy/

Another option is to use a std::set to insert the numbers and then copy them to the vector.
@howdyhowdyhowdy,
You do realise that, with your current parameters, you want 10 distinct random numbers ... between 1 and 10 inclusive? So they must be the numbers 1-10 in some order.

The only distinction between your selections will be the order in which they occur - in which case you should indeed use std::shuffle. By contrast, std::set will automatically sort them into increasing order, although you could put them in a vector after checking that they aren't yet in your std::set.
Using rand() % 9 + 1; is generating numbers in the range 1 - 9, so you're never going to get 10 distinct, single-digit numbers.

Could fill the 10 slots -- using 0 - 9 rather than 1 - 9 -- by random positioning the numbers one at a time? (Or did you want 1 - 10 ?)

Andy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

int main() {
    srand((unsigned)time(NULL));
    
    // create code

    const size_t codelen = 10;

    vector<int> secretcode(codelen, -1);
    
    for (size_t N = 0; N < codelen; ++N) {
        int idx = rand() % 10;
        // if slot already used, move to next one
        while(-1 != secretcode[idx]) {
            idx = (idx + 1) % codelen;
        }
        secretcode[idx] = N;
    }
    
    for(auto M : secretcode) {
        cout << M;
    }
    cout << endl << endl; 
    
    // check code
    
    vector<int> check(secretcode);
    
    std::sort(check.begin(), check.end());
    
    bool ok = true;
    if(0 != check[0]) {
        ok = false;
    } else {
         for (size_t i = 1; i < check.size(); ++i) {
             if(1 != (check[i] - check[i - 1])) {
                 ok = false;
             }
         }
    }
    
    cout << "code is " << (ok ? "OK" : "bad") << endl;     
    
    return 0;
}
Start with a list of numbers from 1 to 10, then run the Fisher-Yates algorithm.

In psuedocode:
1
2
  for (int i = 0; i < v.size() - 1; ++i)
    swap(v[i], v[random_integer_in_halfopen_interval(i, size())]);

Last edited on
Is Fisher-Yates a likely implementation of std::shuffle?
http://www.cplusplus.com/reference/algorithm/shuffle/

without using random shuffle

Philosophically, is that "without simply calling library function that is perfect for this purpose" or "without using the algorithm that library function probably does use"?

The shuffling takes a deck of (unique) cards and shuffles them into random order.


Think of lottery. There are unique, numbered balls. One ball is taken randomly and stored elsewhere. After the ball is taken, there are one less ball in the pot. Hence same number cannot be selected twice.


While shuffling swaps cards, it keeps all cards in the deck.
Selecting a ball in lottery not only points at a ball, but also removes it from the pot.
Both produce a random sequence, but with different method.
Topic archived. No new replies allowed.