dimanche 6 février 2022

Random generator without duplicates [duplicate]

I have this code but it has a very big problem: it duplicates all my output and every time I test it it is duplicate again...

We get billions from this code, but 1 million or so is new and non-duplicate

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


void randomGen(long unsigned *list, unsigned int word_count, unsigned int word_length, unsigned int length_alpha)
{
    unsigned int i;
    long unsigned int r = (long unsigned int)pow(length_alpha, word_length);
    printf("%ld\n", r);
    for (i = 0; i < word_count; i++)
    {
        list[i] = rand() % r;
        printf("%ld\n", list[i]);
    }
}

void writeToFile(const char *filename, long unsigned *list, unsigned int word_count, unsigned int word_length, char *alpha, unsigned int length_alpha, unsigned int _start, unsigned int _end)
{
    unsigned int i, j, k;
    FILE *f = fopen(filename, "w");
    for (i = _start; i < _end; i++) // <word_count
    {
        k = list[i];
        for (j = 0; j < word_length; j++)
        {
            fwrite(alpha + (k % length_alpha), 1, sizeof(char), f);
            k /= length_alpha;
        }
        fwrite("\n", sizeof(char), 1, f);
    }

    fclose(f);
}

void writeToFile2(const char *filename, long unsigned *list, unsigned int word_count, unsigned int word_length, char *alpha, unsigned int length_alpha, unsigned int _start, unsigned int _end)
{
    unsigned int i, j, k;
    FILE *f = fopen(filename, "w");
    for (i = _start; i < _end; i++) // <word_count
    {
        for (j = 0; j < word_length; j++)
        {
            k = rand() % length_alpha;
            fwrite(alpha + (k), 1, sizeof(char), f);
        }
        fwrite("\n", sizeof(char), 1, f);
    }
    fclose(f);
}

int main()
{
    int i;
    unsigned int count, length, length_alpha, limit = 1000000000;
    char *str, *filename;
    long unsigned int *list;
    srand(SEED);

    printf("Enter Word count:");
    scanf("%d", &count);

    printf("Enter length Random Word:");
    scanf("%d", &length);

    printf("Enter Size Alpha:");
    scanf("%d", &length_alpha);

    filename = malloc(12 * sizeof(char));
    str = malloc(length_alpha * sizeof(char));
    printf("Enter Alpha:");
    scanf("%s", str); //"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"

    list = malloc(count * sizeof(long unsigned));

    // randomGen(list, count, length, length_alpha);
    for (i = 0; i < (count / limit); i++)
    {
        sprintf(filename, "random%d.txt", i);
        writeToFile2(filename, list, count, length, str, length_alpha, i * limit, ((i + 1) * limit) - 1);
    }
    if (i * limit < count)
    {
        sprintf(filename, "random%d.txt", i);
        writeToFile2(filename, list, count, length, str, length_alpha, i * limit, count);
    }

    printf("%d\n", i);
    return 0;
}

for example:

gcc randomProgram.c -o randomProgram.out -lm
./randomProgram.out
Enter Word count: 1000000000
Enter length Random Word: 20
Enter Size Alpha:10
Enter Alpha:1234567890



Aucun commentaire:

Enregistrer un commentaire