mercredi 4 novembre 2020

rand() behaves differently between MacOS and Linux

I'm trying to generate a random-number sequence with rand(). I have something like this:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>                                                                                                            


int Random(int min, int max)                                                                                
{                                                                                                           
  /* returns a random integer in [min, max] */                                                              
                                                                                                            
  double uniform; // random variable from uniform distribution of [0, 1]                                    
  int ret; // return value                                                                                  
  srand((unsigned int)clock());                                                                             
                                                                                                            
  uniform = rand() / (double)RAND_MAX;                                                                      
  ret = (int)(uniform * (double)(max - min)) + min;                                                         
                                                                                                            
  return ret;                                                                                               
}                                                                                                           
                                                                                                            
                                                                                                            
int main(void)                                                                                              
{                                                                                                           
  for(int i=0; i<10; i++)                                                                                   
    printf("%d ", Random(0, 100));                                                                          
  printf("\n");                                                                                             
                                                                                                            
  return 0;                                                                                                 
}  

It made different results when executed on MacOS(Mojave) & Ubuntu 18.04.

It works on Ubuntu:

76 42 13 49 85 7 43 28 15 1

But not on MacOS:

1 1 1 1 1 1 1 1 1 1

Why doesn't it work well on MacOS? Is there something different in random number generators?




Aucun commentaire:

Enregistrer un commentaire