12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include <sys/types.h>
- #include <unistd.h>
- #define CELLS_COUNT 3000
- //#define EVENTS_MULTIPLIER 6
- #if CELLS_COUNT > RAND_MAX
- # error CELLS_COUNT > RAND_MAX
- #endif
- int main(int argc, char *argv[]) {
- if (argc <= 1) {
- fprintf(stderr, "syntax: test events_multiplier\n");
- return -1;
- }
- int events_multiplier = atoi(argv[1]);
- long cells[CELLS_COUNT];
- memset(cells, 0, sizeof(cells));
- srand((unsigned) time(NULL) ^ getpid());
- for (int i = 0; i < CELLS_COUNT*events_multiplier; i++) {
- long value = rand() % CELLS_COUNT;
- cells[value]++;
- }
- long average = events_multiplier;
- long derivation = 0;
- for (int i = 0; i < CELLS_COUNT; i++) {
- derivation += fabs(cells[i] - average);
- printf("% 4li", cells[i]);
- if ((i+1) % 50 == 0) {
- printf("\n");
- }
- }
- double average_derivation = ((double)derivation) / CELLS_COUNT;
- printf("\n\naverage:\t\t\t%li\naverage derivation:\t\t%lf\naverage relative derivation:\t%lf\n", average, average_derivation, average_derivation / average);
- return 0;
- }
|