1

I sometimes get the same random numbers with the code below. How can I fix this?

int main()
{
    int numbers[6];
    srand(time(0));
    for(int j=1;j<=6;j++)
    {
        numbers[j]=rand()%10;
        printf("\n%d",numbers[j]);
    }
}
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
  • 5
    By the way, you are writing beyond the array, which is a ticket to trouble. – Yunnosch Dec 02 '19 at 00:20
  • 4
    `j=1;j<=6;` ==> `j=0; j<6;` - start with that. Then describe your *real* problem, which I suspect is you desire a random draw of six *distinct* numbers from a field of ten; i.e. no duplicates. Your *goal* of your code needs to be well-described. – WhozCraig Dec 02 '19 at 00:22
  • If you call the program often with a high frequency, then you predictably get the same sequence of numbers during certain periods of time. Is that what you mean? – Yunnosch Dec 02 '19 at 00:23
  • It is gonna be 49 not 10 but it doesn't matter. When I want 6 number among 10(or 49) I want every one of it to be unique and different from one another. – Ersin Yılmaz Aslan Dec 02 '19 at 00:25
  • 1
    If you roll dice multiple times, you will get repeats of the same numbers. That’s equivalent to what you are doing here. Perhaps you want to _shuffle_ a set of numbers instead? – Arkku Dec 02 '19 at 00:25
  • 1
    If you want non-repeating numbers, then they are actually LESS random. You have to explicitly program that. – Yunnosch Dec 02 '19 at 00:26
  • 2
    If this is to pick lottery numbers, shuffle an array containing all the numbers and print the first 6. Search: Fisher-Yates shuffle in C – Arkku Dec 02 '19 at 00:26
  • 2
    Does this answer your question? [Generating non-repeating random numbers](https://stackoverflow.com/questions/8554292/generating-non-repeating-random-numbers) – Yunnosch Dec 02 '19 at 00:29
  • If your problem is that when you run the program twice in quick succession you get the same sequence of random numbers despite having called `srand`, see [Why does `srand` create same numbers?](https://stackoverflow.com/questions/58580795/why-does-srand-create-same-numbers) – Steve Summit Dec 02 '19 at 00:36

1 Answers1

1

The main issue is that your range is kind of small, so duplicates, while they aren't necessarily common, they'll crop up enough to annoy you.

I've got two suggestions, the first being to increase the range of your random numbers

int main(void) {
    int nums[6];
    srand(time(NULL));

    for (int i = 0; i < 6; ++i) {
        nums[i] = rand() % 100; // range has been increased
        printf("%d\n", nums[i]);
    }
}

Another way to absolutely ensure unique numbers would be to check if your array already contains the number before adding it. As you might guess, depending on the way you implement it, time complexity will become a factor.

int main(void) {
    int nums[6];
    srand(time(NULL));

    for (int i = 0; i < 6; ++i) {
        int temp = rand() % 10;
        bool exists = false;
        for (int j = 0; j < i; ++j) {
            if (nums[j] == temp) {
                exists = true;
                break;
            }
        }

        if (!exists) {
            nums[i] = temp;
            printf("%d\n", nums[i]);
        }
        else {
            --i; // force the loop back
        }
    }
}
Frontear
  • 1,150
  • 12
  • 25