Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <signal.h>
- #include <stdio.h>
- #include <time.h>
- #define N 16
- static int done = 0;
- static void end(int sig)
- {
- done = 1;
- }
- int main(int argc, char **argv)
- {
- time_t seed = time(NULL);
- if (argv[1]) {
- seed = atoi(argv[1]);
- }
- srand(seed);
- // Set the handler so we finish cleanly
- signal(SIGINT, end);
- int width = 0;
- {
- int c = N;
- while (c) {
- width++;
- c /= 10;
- }
- }
- // Print initial layout
- for (int i = 0; i < N; i++) {
- printf("%*d: 0\n", width, i);
- }
- unsigned long hits[N] = {};
- unsigned long n = 0;
- while (!done) {
- n++;
- printf("\r\e[%dA", N);
- hits[rand() % N]++;
- for (int i = 0; i < N; i++) {
- printf("%*d: %f\n", width, i, (double)hits[i] / n);
- }
- printf("Iterations: %lu", n);
- }
- // Print one last time for cleanliness
- printf("\r\e[%dA", N);
- for (int i = 0; i < N; i++) {
- printf("%*d: %f\n", width, i, (double)hits[i] / n);
- }
- printf("Iterations: %lu\n", n);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement