Random Number Generator

Ok, Im helping out my local lan gaming center and i'm trying to make a random number generator to place people onto different stations. There are 12 stations and here is the layout
12
11
10
9
8
7
6
5
4
3
2
1

Each person is already assigned a number and we want the program to spit out in order what station they will be at.
// This program uses the random number generator to show
// a listing of random numbers on the screen
// Written by Janine Bouyssounouse on 10/15/08
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <time.h>
using namespace std;
void welcome();
int randomNumber();
void displayRandom(int number);
int main(int argc, char *argv[])
{
int x;
int spot1;
int spot2;
int spot3;
int spot4;
int spot5;
int spot6;
int spot7;
int spot8;
int spot9;
int spot10;
int spot11;
int spot12;
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);

spot1=rand()%(12-1+1)+1;
cout<<"number 1 goes to " <<spot1;
cout<<endl;
spot2=rand()%(12-1+1)+1;
cout<<"number 2 goes to " <<spot2;
cout<<endl;

system("PAUSE");
return 0;
}



at the moment thats what i have, it does work to an extent but the numbers repeat and we can't have them repeating. I used to be good at this but i havent written a program in over 3 years and am just starting to try and pick it back up. Any information on how to get all

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
int spot1;
int spot2;
int spot3;
int spot4;
int spot5;
int spot6;
int spot7;
int spot8;
int spot9;
int spot10;
int spot11;
int spot12; 

instead of this,
use this
 
int spots[12];

as an easy way , you can do it as
1
2
3
4
5
6
7
#include <algorithm>
..
...
int spots[12];
for( int i = 0 ; i < 12 ; i++)
        spots[i] = i;
random_shuffle( spots , spots+12);

Last edited on
mmk i couldnt get that to display but this is what i have now

#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <time.h>
#include <algorithm>
using namespace std;
int main(int argc, char *argv[])
{
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);
int i;//spot
int p;//player
int spots[12];
i=0;
p=1;
while(i<12)
{

spots[i]=rand()%(12-1+1)+1 ;
cout<<"player "<<p<<" is at station "<<spots[i];
i=i+1;
p=p+1;
cout<<endl;
}



system("PAUSE");
return EXIT_SUCCESS;
}




Its printing all 12 spots but the numbers repeat. what can i do to stop them from repeating
I was also thinking maybe it would just be easier to make an array then shuffle that and display it. It doesnt need to say player [p] is at station[i]. It just needs to not repeat and display 12 numbers on the screen at one time.

Whatever the easiest way to do it will be best. Thanks
Topic archived. No new replies allowed.