8 Replies - 634 Views - Last Post: 12 February 2014 - 12:57 PM Rate Topic: -----

#1 ninjawesome222  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 106
  • Joined: 27-January 10

Generate Unique Numbers in C

Posted 11 February 2014 - 02:52 PM

Here is what I'm trying to accomplish (it is a rather simple program): A classroom of students are to grade a certain number of other exams. The exams should be distributed equally and RANDOMLY, every student should receive the same number of exams, and no student should receive their own exam to grade. The only problem I have is to generate unique random exams for each student. Right now, I have it set to where each exam is distributed the same number of times, every student gets the same number of exams to grade, and no one gets there own. However, I don't have any parameters that prevent one student from getting the same exam multiple time.

Here is an example output:

Student 1 will grade: 4 3 2 5 <- CORRECT OUTPUT (no exam appears more than once)
Student 2 will grade: 5 5 5 1 <- exam 5 appears three times
Student 3 will grade: 4 2 2 2 <- exam 2 appears three times
Student 4 will grade: 3 3 1 1 <- exams 3 and 1 each appear twice
Student 5 will grade: 1 3 4 4 <- exam 4 appears twice
(each exam appears four times and every student is assigned four exams. no one gets their own)

Here is my code (area of problem is close to the bottom):
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void create_class (int);

int main (void) {
    srand(time(NULL));
    
// ASSIGN TOTAL NUMBER OF STUDENTS
    int students;
        
    printf ("Number of students: ");
    scanf  ("%d", &students);

// ASSIGN EXAM LIMIT 
	int limit;
	  
    printf ("Number of exams per student: ");
    scanf  ("%d", &limit);
    
// ASSIGN EXAM DISTRIBUTION
	int distribution;
	
	distribution = students / limit;
	printf ("distribution: %d\n\n", distribution);

// ASSIGN NUMBERS TO EVERY STUDENT 
    int i;
    int number;
    int class[students];
    
    for (i = 0; i < students; i++) {
    	printf ("Student %d Exam Number: ", i + 1);
    	scanf  ("%d", &number);
    	class[i] = number;
    }

// DECLARE COUNT ARRAY
	int count[students];
	for (i = 0; i < students; i++) {
		count[i] = 0;
	}

// DECLARE EXAMS ARRAY
	int exams[limit];
	for (i = 0; i < limit; i++) {
		exams[i] = 0;
	}
	
// ASSIGN EXAMS TO EVERY STUDENT
	int random;
	
	for (i = 0; i < students; i++) {
		printf ("\n\nStudent %d will grade: ", class[i]);
	
		int j;

// THIS IS WHERE I NEED HELP GENERATING UNIQUE NUMBERS
		for (j = 0; j < limit; j++) {
			random = rand() % students;
			while (count[random] >= limit || class[random] == class[i]) {
				random = rand() % students;
			}
			count[random] += 1;
			printf ("%d ", class[random]);
		}
	}
    
    getch();
    return 0;
}
    
    



I tried keeping the exams for each student in the array exam and then checking each one every time I generate a number, but that didn't work. I've been spending hours trying to figure this out, so any help is greatly appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: Generate Unique Numbers in C

#2 Countdown  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 10
  • Joined: 16-November 09

Re: Generate Unique Numbers in C

Posted 11 February 2014 - 03:12 PM

View Postninjawesome222, on 11 February 2014 - 02:52 PM, said:

I tried keeping the exams for each student in the array exam and then checking each one every time I generate a number, but that didn't work. I've been spending hours trying to figure this out, so any help is greatly appreciated


I think you were on the right track. I did something similar in a program awhile back and I used an array. You probably just have to do some debugging on the code to get it working.
Was This Post Helpful? 0
  • +
  • -

#3 Ricky65  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 115
  • Joined: 03-June 10

Re: Generate Unique Numbers in C

Posted 11 February 2014 - 05:05 PM

View Postninjawesome222, on 11 February 2014 - 02:52 PM, said:

// DECLARE COUNT ARRAY
int count[students];
for (i = 0; i < students; i++) {
count[i] = 0;
}

That's a bit of a convoluted way of initializing all the elements in an array to 0. You can just do this instead for brevity:
int count[students] = {0}; 

Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3099
  • View blog
  • Posts: 10,888
  • Joined: 08-August 08

Re: Generate Unique Numbers in C

Posted 11 February 2014 - 07:05 PM

Question about your "correct" output: How do you know what student took each exam, and therefore that the student grading it isn't grading their own exam?
Was This Post Helpful? 0
  • +
  • -

#5 ninjawesome222  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 106
  • Joined: 27-January 10

Re: Generate Unique Numbers in C

Posted 12 February 2014 - 11:46 AM

View PostCTphpnwb, on 11 February 2014 - 07:05 PM, said:

Question about your "correct" output: How do you know what student took each exam, and therefore that the student grading it isn't grading their own exam?


Perhaps my description was not clear enough, my apologies. When the output says student 1 will grade exams 4, 3, 2, and 5, I mean he will be grading student 4's exam, student 3's exam, student 2's exam, and student 5's exam.
Was This Post Helpful? 0
  • +
  • -

#6 ninjawesome222  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 106
  • Joined: 27-January 10

Re: Generate Unique Numbers in C

Posted 12 February 2014 - 11:49 AM

I am having difficulty generating random numbers that are unique. What I have attempted to do is generate a random number, store it in an array, and then when I generate the next random number, I check each slot of the array for duplicates. However, when I run this code, it still yields duplicates and sometimes there is not output at all. Any help is greatly appreciated.

Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main (void) {
	int limit = 4;
	int students = 5;
	int exams[4];
	srand(time(NULL));
	int class[5] = {1,2,3,4,5};
	
	int random;
	int i, j;
	int flag = -1;
	
	for (i = 0; i < limit; i++) {
		flag = 1;
		random = rand() % students;
		do {
		for (j = 0; j < limit; j++) {
			if (class[random] == exams[random]) {
				j = 0;
				flag = -1;
				random = rand() % students;
			}
		}
			} while (flag == -1);
			exams[i] = class[random];
	}
	
	for (i = 0; i < 4; i++) {
		printf("%d ", exams[i]);
	}
	
	getch();
	return 0;

}


Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10804
  • View blog
  • Posts: 40,277
  • Joined: 27-December 08

Re: Generate Unique Numbers in C

Posted 12 February 2014 - 11:52 AM

With over 100 posts, you should know the rules. Do not open duplicate threads.

Edit: Merged threads per discussion with the OP.

This post has been edited by macosxnerd101: 12 February 2014 - 12:04 PM

Was This Post Helpful? 0
  • +
  • -

#8 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 625
  • Joined: 11-June 11

Re: Generate Unique Numbers in C

Posted 12 February 2014 - 12:40 PM

Well! First, there are 5 student and 1 exams array, shouldn't every student be having it's own exam array? As you explained in the original post that student 1 grades student 4, 3, 2, and 5. So will the other students do also...

You might need a multidimensional array for that Array[student][Exams]...

And, would you explain what this code is doing? I don't understand what it affects...
do {
	for (j = 0; j < limit; j++) {
		if (class[random] == exams[random]) {
			j = 0;
			flag = -1;
			random = rand() % students;
		}
	}
} while (flag == -1);



Hope it Helps!
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3099
  • View blog
  • Posts: 10,888
  • Joined: 08-August 08

Re: Generate Unique Numbers in C

Posted 12 February 2014 - 12:57 PM

View Postninjawesome222, on 12 February 2014 - 01:49 PM, said:

I am having difficulty generating random numbers that are unique.

Unless there are a very large amount of numbers I usually create an array containing all the numbers I'll need and then iterate through each element and randomly swap it with some other element. Then it's just a matter of iterating through the array to pick unique yet random values.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1