creating a unique random number generator

Hey everyone, so I am trying to have this program randomly generate 10 student id numbers that are 1 through 10 but I do not want there to be any repeating numbers. The scores one is just fine but I can not seem to figure out how to get the ID numbers to not repeat. anyone have some advice or help?

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
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include <stdio.h>
#include <time.h>


struct student{
    int id;
    int score;
};

struct student* allocate(){

     struct student *stud =  malloc(10*sizeof(struct student));  //Allocate memory for 10 students
     assert (stud !=0);

     return stud;
}

void generate(struct student *students){
     srand(time(NULL));

                                      // Generate random ID numbers
           for(int j=0; j<10; j++){
              students[j].id = (rand()%10+1);
            }

                                             //Generate random scores
           for(int j=0; j<10; j++){
           students[j].score = (rand()%100+1);
       }
     }

void output(struct student* students){

     for(int j=0; j<10; j++){
        printf("ID-%d Score-%d\n", students[j].id, students[j].score);
        }
}


void summary(struct student* students){

     int mins = 100;
     int maxs = 0;
     int avgs = 0;
 for(int i=0; i<10; i++){
        if(students[i].score < mins){
            mins = students[i].score;
        }
        if(students[i].score > maxs){
            maxs = students[i].score;
        }
        avgs = avgs + students[i].score;
    }

    avgs = avgs/10;

    printf("\nMinimum score: %d\nMaximum score: %d\nAverage score: %d\n", mins, maxs, avgs);

}


void deallocate(struct student* stud){

   free (stud);

}

int main(){

    struct student *stud = NULL;

    stud = allocate();  //allocate function call

    generate(stud);     //generate function call

    output(stud);       //output function call

    summary(stud);      //summary function call

    deallocate(stud);   //deallocate function call

    return 0;
}



Thank you guys!
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 <cstdlib>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <iomanip>

struct student
{
    int id;
    int score;
};

// generate 'n' random unique ids in [1,n],
// random non-unique scores in [0,100]
void generate( student* students, std::size_t n )
{
    // generate ids in sequence 1, 2, 3, 4, ... n
    // generate random scores in [0,100]
    for( std::size_t i = 0 ; i < n ; ++i )
    {
        students[i].id = i+1 ;
        students[i].score = std::rand() % 101 ;
    }

    // form a random permutation of the students
    // http://www.cplusplus.com/reference/algorithm/random_shuffle/
    std::random_shuffle( students, students+n ) ;
}

int main()
{
    std::srand( std::time(nullptr) ) ; // seed just once, at the beginning of main

    constexpr std::size_t N = 15 ;
    student students[N] ;

    generate( students, N ) ;

    for( const student& s : students )
        std::cout << std::setw(2) << s.id << ". " << std::setw(3) << s.score << '\n' ;
}

http://coliru.stacked-crooked.com/a/569f327d82cc0403
Topic archived. No new replies allowed.