mardi 19 avril 2022

Is there an efficient way to use random number deduplication?

I making a program that uses random numbers. I wrote the code as below, but the number of loops was higher than I expected Is there an efficient way to use random number deduplication?

#include <iostream>
#include <cstdlib>
#include <ctime>
#define MAX 1000
int main(void)
{
    int c[MAX] = {};
    int i, j = 0;
    srand((unsigned int)time(NULL));
    for (i = 0; i < MAX; i++)
    {
        c[i] = rand() % 10000;
        for (j = 0; j < i; j++)
        {
            if (c[i] == c[j])
            {
                i--;
                break;
            }
        }
    }
    for (i = 0; i < MAX; i++)
    {
        std::cout << c[i] << std::endl;
    }
    return 0;
}



Aucun commentaire:

Enregistrer un commentaire