#include #include #include #define QUICK_MOD 1 int walk (int n, int test, int *count) { int *visited, i; visited = (int*) malloc(n * sizeof(int)); visited[0] = 0; srand(time(NULL)); for (i = 0; i < test; i++) { int need_visit, now, j; for (j = 1; j < n; j++) visited[j] = 1; now = 0; need_visit = n - 1; while (need_visit != 0) { if (rand() > RAND_MAX / 2) { #if QUICK_MOD now++; if (now == n) now = 0; #else now = (now + 1) % n; #endif } else { #if QUICK_MOD if (now == 0) now = n - 1; else now--; #else now = (now + (n-1)) % n; #endif } need_visit -= visited[now]; visited[now] = 0; // printf("%d ", now); } // printf("\n"); count[now]++; } free(visited); } int main (int argc, char **argv) { int *count, n, test, i; if (argc < 2) { printf("Usage: %s n test\n", argv[0]); return -1; } n = atoi(argv[1]); test = atoi(argv[2]); printf("n = %d, test = %d\n", n, test); count = (int*) malloc(n * sizeof(int)); for (i = 0; i < n; i++) count[i] = 0; walk(n, test, count); for (i = 0; i < n; i++) { printf("%2d: %d\n", i, count[i]); } return 0; }