Keeping track of multiple random numbers

I am making a game with C++ and i need to generate 5 random numbers, and keep track of them. I have tried searching for other people trying to do this but i haven't found anyone similar. Anyway, here is the current code of the random thingy:

1
2
3
4
5
6
void roll() {
srand(time(0));
for (int x = 1; x <= 5; x++) {
cout << (rand() % 6 + 1) << endl;
  }
}
Last edited on
Store them in a container:
1
2
3
4
5
6
7
8
9
10
11
12
void roll()
{
    srand(time(0));

    std::vector<int> numbers;
    for (int x = 1; x <= 5; x++)
    {
        n = rand() % 6 + 1;
        numbers.push_back(n);
        cout << n << endl;
    }
}
Thank you for your reply, however I want to keep track of all 5 numbers in 5 different variables. I guess I wasn't clear enough, sorry.
And also I have never worked with vector, so im kinda lost in that2.
What do you mean track 5 random numbers?

if you want to generate 5 random numbers and "track" them, you need to make an array where each index holds a single random generated number
however I want to keep track of all 5 numbers in 5 different variables.
That's what I did. If you take the time to find out what a vector is, you'll see it has stored your 5 random numbers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <vector>
#include <iostream>
#include <iterator>
#include <stdlib.h>

std::vector<int> roll(int n)
{
    std::vector<int> numbers;
    for (int i = 0; i < n; ++i)
        numbers.push_back(rand() % 6 + 1);

    return numbers;
}

int main()
{
    srand(2015);
    auto n = roll(5);
    std::copy(n.begin(), n.end(), std::ostream_iterator<int>(std::cout, " "));
}
Last edited on
Topic archived. No new replies allowed.