0
Can you give a simple explanation and an example for random function.
examples and explanation
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
+ 1
Do you think about the rand() function in C?
0
#include<stdlib.h>
#include<stdio.h>
int main(){
int a=rand()%50;
printf("%d",a);
}
Just example for random