How to use srand and rand functions in C

2 Answers

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> 
#include <time.h>
  
int main(void) {
    srand((unsigned int) time(0) + getpid());
 
    int n = rand();
 
    printf("%d\n", n);
     
    return 0;
}
  
     
     
     
/*
run :
 
1960361416
 
*/

 



answered Feb 15, 2021 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> 
#include <time.h>
 
int main(void) {
    srand(time(0)); 

    int n = rand();

    printf("%d\n", n);
    
    return 0;
}
 
    
    
    
/*
run :

2018391676

*/

 



answered Feb 15, 2021 by avibootz

Related questions

1 answer 126 views
1 answer 159 views
159 views asked Dec 10, 2020 by avibootz
1 answer 169 views
169 views asked May 14, 2024 by avibootz
1 answer 200 views
200 views asked Feb 15, 2017 by avibootz
...