random number generator

The program is taking an inputed amount of teams, and placing them on a bracket line for a tournament. It prints correctly, the only problem is that the numbers repeat. I need a way to check for the random number or to just fill the array with x amount of teams and then shuffle it and print it, anything at all will be helpful. Thanks




#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <time.h>
#include <algorithm>
using namespace std;
int main(int argc, char *argv[])
{
int x;
cout<<"how many teams are in the tournament?"<<endl;
cin>>x;
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);
int i;//spot
int t;//team

int spots[x];
i=0;
t=1;
while(i<x)
{

spots[i]=rand()%(x-1+1)+1 ;
cout<<"Team "<<t<<" is on bracket line "<<spots[i];
i=i+1;
t=t+1;
cout<<endl;
}



system("PAUSE");
return EXIT_SUCCESS;
}
Fill up spots with numbers 1-x, then use std::random_shuffle to shuffle them.

Other advices:
#include <cstdlib> and <ctime> instead of stdlib.h and time.h

Seed the random number generator like this : srand(time(0));

Don't use variable sized static arrays, few compliers support it, but it's not standard. Use dynamic arrays or std::vector instead.

Use for loop, it makes your code more readable.

Aviod system().
If you want to avoid the console from colsing use std::cin.ignore(std::number_limits<std::streamsize>::max(), '\n');*

And um, (x-1+1) == x :)

*See Duoas' post here http://cplusplus.com/forum/beginner/1988/#msg7263
Topic archived. No new replies allowed.