random number inside vector

hello

how do i insert randomized elements inside a vector without repeating elements?
i came about this article that I thought would help me but i think I'm implementing incorrectly?


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
#include <iostream>
#include <vector>

using namespace std;

//randomized numbers
int random_number(int lo, int hi) {
	return rand() % (hi - lo) + lo;
}

int main(){
    
     vector<int> list3;
    srand(time(NULL));

    for (int i = 1; i <= 100; i++)
    {
        if (count(list3.begin(), list3.end(), random_number(1, 100)))
            i - 1;
        else
            list3.push_back(random_number(1, 100));
    }
    cout << "Random list: " << endl;
    cout << "BEFORE SORT: ";
    for (auto& i : list3)
    {
        cout << i << " ";
    }
    cout << endl;

    sort(list3.begin(), list3.end());
    cout << "AFTER SORT: ";
    for (auto& i : list3)
    {
        cout << i << " ";
    }

return 0;
}

Last edited on
Dude, get your code to compile first. count(), sort() are from #include <algorithm>
@ElusiveTau it compiles on mine without that... but yes you're correct
Last edited on
how do i insert randomized elements inside a vector without repeating elements?

One method is to create a vector filled with unsorted numbers and then shuffle the vector's elements to be randomly sorted.
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
#include <iostream>
#include <vector>
#include <random>
#include <numeric>

int main()
{
   // let's create a random number engine
   std::default_random_engine rng;

   // let's seed the sucka
   rng.seed(std::random_device {}());

   // let's create a vector that holds 100 numbers
   std::vector<int> orig_vec(100);

   // let's insert 1 to 100 into the vector
   std::iota(orig_vec.begin(), orig_vec.end(), 1);

   unsigned count { };

   std::cout << "The unsorted vector:\n";
   for (const auto& itr : orig_vec)
   {
      std::cout << itr << '\t';

      count++;
      if (count % 10 == 0) { std::cout << '\n'; }
   }
   std::cout << '\n';

   // let's create a clone vector so the original stays intact
   std::vector<int> shuffled_vec(orig_vec);

   // let's shuffle the vector elements randomly
   std::shuffle(shuffled_vec.begin(), shuffled_vec.end(), rng);

   count = 0;

   std::cout << "The shuffled vector:\n";
   for (const auto& itr : shuffled_vec)
   {
      std::cout << itr << '\t';

      count++;
      if (count % 10 == 0) { std::cout << '\n'; }
   }
   std::cout << '\n';
}

The unsorted vector:
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      52      53      54      55      56      57      58      59      60
61      62      63      64      65      66      67      68      69      70
71      72      73      74      75      76      77      78      79      80
81      82      83      84      85      86      87      88      89      90
91      92      93      94      95      96      97      98      99      100

The shuffled vector:
38      5       37      79      55      47      27      21      43      81
91      6       93      96      29      69      89      99      90      20
14      30      65      56      78      74      12      40      94      44
84      24      3       33      17      51      64      97      85      62
98      26      77      48      36      59      53      13      54      92
75      18      61      42      86      1       23      72      80      52
4       100     7       31      9       60      71      63      34      70
50      83      28      68      88      57      22      32      82      39
45      76      67      19      8       46      2       10      49      95
35      58      16      41      87      66      11      73      15      25

ElusiveTau is correct, including the proper header is the correct and portable method. It's not safe to assume other standard headers implicitly include <algorithm>.
Last edited on
The random number on line 18 and line 21 are not necesssarily the same. Fix that by generating a random number once:

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
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int random_number(int lo, int hi)
{
	return rand() % (hi - lo) + lo;
}

int main()
{    
    srand(time(nullptr));

    vector<int> v;
    for (int i = 0; i < 100; ++i)
    {
        int r = random_number(1, 100);
        if (count(v.begin(), v.end(), r))
            i -= 1;
        else
            v.push_back(r);
    };
    
    cout << "Random List\nBEFORE SORT: ";
    for (auto& i : v) 
        cout << i << ' ';
    cout << '\n';

    sort(v.begin(), v.end());
    
    cout << "AFTER SORT:  ";
    for (auto& i : v)
        cout << i << " ";
    cout << '\n';
}


Problem is, your algorithm is too slow to be practical. The first non-duplicate random number is always chosen on the first try. But when it's time to insert the last non-duplicate number, that one will only be chosen with 1% probability. Each time a guess is taken, the entire vector must be checked to look for a duplicate.

What you want to do is fill the vector with sequential integers from 1 to 100, then use the Fisher-Yates algorithm.
http://www.cplusplus.com/forum/beginner/248983/#msg1097127

SA @Furry Guy's post above. std::shuffle is most commonly an implementation of Fisher's algorithm.
totoo, please also read this thread: http://www.cplusplus.com/forum/beginner/248737/
It shows some possibilities for ways to have non-repeating random numbers.
Last edited on
Topic archived. No new replies allowed.