mercredi 7 novembre 2018

Using assert to make a random generator without similar numbers

so my question is how to create a random generator without having duplicates while using assert.

the assert part is important and I haven't been able to find anything on stack overflow since I am pretty new to C++ I would like to know about this.

#include "stdafx.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <assert.h>
    #include <time.h>
        int test_assert(int x)
        {
            assert(x <= 46);
            return x;
        }

        int main()
        {
            int i,n;
            int numbers[47];
            time_t t;
            n = 20;

            /* Intializes random number generator */
            srand((unsigned) time(&t));

            for (i = 0; i < n; i++)
            {
                test_assert(i);
                printf("i = %d\n", rand() % 47);

                if (i == numbers[i]) {
                    printf("equal numbers detected");
                    printf("i = %d\n", rand() % 47);
                    //TO DO Check if it's random then run random again
                }
            }
            while (1) {

            }
            return 0;
        }




Aucun commentaire:

Enregistrer un commentaire