1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/* http://mapage.noos.fr/clib/ed/inc/random.h */
#include "ed/inc/random.h"
#define NB_TIRAGES 100000
#define MAX 9
static void clr_count (unsigned long count[], size_t n)
{
size_t i;
for (i = 0; i < n; i++)
{
count[i] = 0;
}
}
static void prt_count (char const *cmt, unsigned long const count[], size_t n)
{
size_t i;
printf ("%s\n", cmt);
for (i = 0; i < n; i++)
{
printf ("[%2lu] = %lu\n", (unsigned long) i, count[i]);
}
printf ("\n");
}
int main ()
{
unsigned long count[MAX + 1];
srand ((unsigned) time (NULL));
/* % */
{
int i;
clr_count (count, sizeof count / sizeof *count);
for (i = 0; i < NB_TIRAGES; i++)
{
size_t n = (rand () % (MAX + 1));
count[n]++;
}
prt_count ("Methode naive (Modulo)", count, sizeof count / sizeof *count);
}
/* random Borland C */
{
int i;
clr_count (count, sizeof count / sizeof *count);
for (i = 0; i < NB_TIRAGES; i++)
{
size_t n = random (MAX + 1);
count[n]++;
}
prt_count ("random() de Borland C", count, sizeof count / sizeof *count);
}
/* FAQ DVP */
{
int i;
clr_count (count, sizeof count / sizeof *count);
for (i = 0; i < NB_TIRAGES; i++)
{
size_t n = (int)((float)rand() / RAND_MAX * (MAX + 1 - 1));
count[n]++;
}
prt_count ("FAQ DVP", count, sizeof count / sizeof *count);
}
/* FAQ fclc
*/
{
int i;
clr_count (count, sizeof count / sizeof *count);
for (i = 0; i < NB_TIRAGES; i++)
{
size_t n = (int)((double)rand() / ((double)RAND_MAX + 1) * (MAX + 1));
count[n]++;
}
prt_count ("FAQ fclc ", count, sizeof count / sizeof *count);
}
return 0;
} |
Partager