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 211 views
1 answer 238 views
238 views asked Dec 10, 2020 by avibootz
1 answer 245 views
245 views asked May 14, 2024 by avibootz
1 answer 280 views
280 views asked Feb 15, 2017 by avibootz
...