-1

I have written a code for randomly printing a number from 1 to 10 without any repetition, but it isn't working properly sometimes I get the number that is already written. In short, I'm trying to print numbers from 1-10 randomly with no repetition.

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

main() {
    int no = 0, repeat[100] = { 0 }, i = 0, x = 0, j = 0;
    
    srand(time(NULL));
    while (true) {
        no = (rand() % 10) + 1;
        for (i = 0; i < 100; i++) {
            if (no != repeat[i]) {
                x = 1;
            } else if (no == repeat[i]) {
                x = 0;
            }
        }
        if (x == 1) {
            repeat[j] = no;
            printf("\n%d", repeat[i]);
            j = j + 1;
        }
        getch();
    }
}
pradeexsu
  • 1,029
  • 1
  • 10
  • 27
  • 1
    Take an array holding the numbers 1 through 10. Shuffle it (look up Fisher-Yates). Print out as much of the array as desired. – Shawn Dec 13 '20 at 02:48
  • 1
    As soon as you find a repeat you should choose another number. If you keep going through the array you'll find numbers that don't match and reset your repeat flag. A better way to do this is to generate all of the numbers you want to choose from, shuffle that array, then pick them in order. – Retired Ninja Dec 13 '20 at 02:49
  • The problem is where you set the ```x``` variable, you override this each loop, so only the last will count. Starting it outside with ```1``` and only change to ```0``` when it is repeated will fix the problem. But you should review all source code as the comments above suggested. – Matheus Rossi Saciotto Dec 13 '20 at 03:02
  • ```break;``` is what you need to leave a loop when finding a match. – paleonix Dec 13 '20 at 03:02

2 Answers2

2

Don't use rand() to generate the numbers directly, instead fill a sequential array, and then use rand() to shuffle the array, e.g.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/** shuffle integer array of size 'n'
 *  (using fisher-yates method)
 */
void shuffle (int *a, int n)
{
    int i, tmp;
    while (n-- > 1) {
        i = rand() % (n + 1);
        tmp  = a[i];
        a[i] = a[n];
        a[n] = tmp;
    }
}

int main (void) {
    
    int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    
    srand (time(NULL));
    
    shuffle (arr, 10);
    
    for (int i = 0; i < 10; i++)
        printf (" %d", arr[i]);
    putchar ('\n');
}

By shuffling the array and swapping random elements within it, you eliminate all possibility of a duplicate number.

Example Use/Output

$ ./bin/shuffle_arr
 3 10 7 8 4 5 6 9 1 2
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
0
#include <stdio.h>

#define N1 1
#define N2 10

void main() {
    int len = N2 - N1 + 1, i, r, temp;
    int num[len];

    //Fill array with numbers
    for (temp = 0, i = N1; temp < len; i++, temp++)
        num[temp] = i;

    srand(time(NULL));

    for (i = len - 1; i > 0; i--) {
        r = rand() % i; //pop random number
        //swaping
        temp = num[i];
        num[i] = num[r];
        num[r] = temp;
    }

    /*Random Numbers are stored in Array*/

    //print that array
    for (i = 0; i < len; i++)
        printf("%d\n", num[i]);
}
pradeexsu
  • 1,029
  • 1
  • 10
  • 27