Fonction rand() | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Fonction rand()

Ever time I excused program is give me same number why? It's give me like this : le point is (34.56 , 34.56 ) #include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> double reel(){ srand(time(NULL)); double s=rand()%2==0?-1:1; return s*sqrt(rand()%1001); } typedef struct { double abs; double ord; } point ; point RandomPoint(){ point p; p.abs=reel(); p.ord=reel(); return p; } int main(){ point p=RandomPoint(); printf(" le point is (%.2lf , %.2lf )",p.abs,p.ord); return 0; }

22nd Oct 2021, 12:24 PM
HICHAM
6 Answers
+ 2
I made some changes from your code https://code.sololearn.com/cjQfVPIl8heS/?ref=app
22nd Oct 2021, 2:02 PM
Ipang
+ 1
Put this as first line in main() before call to RandomPoint() srand( time( NULL ));
22nd Oct 2021, 1:05 PM
Ipang
+ 1
Thank you I understand
22nd Oct 2021, 2:09 PM
HICHAM
0
Typos: #include <stdio.h> // <studio.h> double reel() // Double (Edit) OP corrected the typos in thread's Description Seed the RNG in the main() function, not in reel() function. srand( time( NULL ) ); // put this in main() function.
22nd Oct 2021, 12:35 PM
Ipang
0
Do i have put RNG in the main but fonction RandomPoint () is defined with fonction real() i did put srand(time(NULL)); but it's give me same number like le point is (-28.72 , -28.72)
22nd Oct 2021, 1:00 PM
HICHAM
0
I did it, i put srand (time(NULL)); in first line in main() but same problem same number.
22nd Oct 2021, 1:16 PM
HICHAM