mercredi 30 janvier 2019

Instead of random, it generates sequence of numbers

I want to generate x number of random numbers and everything works except it gives me sequence of numbers.

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

int main(){


int x;
printf("How many  elements do you want?\n");
scanf("%d", &x);
int *array = (int *)malloc(x * sizeof(int));
srand(time(NULL));
for(int y = 0; y < x; y++){

    *(array + y) = 2 +  rand() % 99;
}

for(int y = 0; y <x; y++){
    printf("Array element %d: %d\n", y, (*array+ y));

}

return 0;}

It gives the numbers like; 27 28 29 30 31, only the first number is random.




Aucun commentaire:

Enregistrer un commentaire