Hi, I'm trying to generate a random number betwen any numbers (eg from 1 to 100) in C and I've tried this code:
It does not give me random numbers. It gives me always the same number for small ranges and it follows a pattern for a graters ranges.Code:#include<stdio.h> #include<math.h> #include<time.h> #include <stdlib.h> int NumAleatori(int max) { int num; num=((int)ceil(((double)rand()/RAND_MAX)*(max+1)))%(max+1); return num; } void main () { int N,a; srand( (unsigned)time( NULL ) ); printf(“Enter an integer:\”); scanf(“%d”,&N); a = NumAleatori(N); printf(“%d”,a); }
Do you know how can I easily solve this?
thanks in advance!