samedi 13 janvier 2018

C: generate array with random numbers in range (allwowing duplicates)

I want to write a C function that gets: a seed, int n (the number of random ints to generate), and upper limit (the max allowed number).

I came so far as to have somthing like this:

// I need a function definition here
// I forgot how to allocate the int array ... somehting with 'sizeof'?
srand(time(NULL));   //seed for rand    
for(i = 0; i < length; i++)
        array[i] = rand()%upperLimit;
return array;

Then I probably need those header files:

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




Aucun commentaire:

Enregistrer un commentaire