0

Can you give a simple explanation and an example for random function.

examples and explanation

24th Sep 2017, 10:09 AM
L PRASANTH
L PRASANTH - avatar
3 Réponses
+ 3
@Aleksey, your solution is not really random, it will always give the same result each time you run your program, this is the "really" random correction : #include <time.h> #include <stdio.h> #include <stdlib.h> int main(){ unsigned i; srand(time(0)); for(i = 0; i < 10; ++i) printf("%d\n", rand()); return 0; } rand : return the next "random" number srand : initialize the seed as the value given in parameter, the seed initialize the starting point (same seed used twice == twice the same result) time : return the number of seconds spent since the first of January 1970, this function is used to obtain a seed different each time, which simulate random
25th Sep 2017, 7:38 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
Do you think about the rand() function in C?
24th Sep 2017, 10:17 AM
Hajnal Máté
Hajnal Máté - avatar
0
#include<stdlib.h> #include<stdio.h> int main(){ int a=rand()%50; printf("%d",a); } Just example for random
24th Sep 2017, 10:33 AM
Aleksey Beliy
Aleksey Beliy - avatar