Last active
November 5, 2026 22:28
-
-
Save usr-ein/e635dbf4b082bc9c00e3282c6836f656 to your computer and use it in GitHub Desktop.
Mutex-based RNG in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // mutex_rng.c — "RNG" from mutex contention: who grabs the lock when the holder lets go? | |
| // Could I make an actual RNG in programming (not pseudo) using mutex? | |
| // | |
| // One thread takes the lock (thread 0), n threads (1st, 2nd....nth) | |
| // then loop-wait to try to get the lock. Whenever thread 0 lets go of | |
| // the lock, it's anyone's guess which of the n threads get it. | |
| // It's based on the randomness of the scheduler I imagine. In practice, | |
| // since they're all looping to get the lock, and we don't know which | |
| // will get it first, wouldn't that be real randomness? | |
| // | |
| // I was watching https://www.youtube.com/watch?v=BmBw827GgFA | |
| // and thought to try this. | |
| // This is also a good resource on the topic: | |
| // https://www.youtube.com/watch?v=dQw4w9WgXcQ | |
| // | |
| // Build: cc -O2 -pthread mutex_rng.c -o mutex_rng | |
| // Run: ./mutex_rng [threads=4] [rounds=20000] [mode: spin|block] | |
| #include <pthread.h> | |
| #include <stdatomic.h> | |
| #include <stdint.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; | |
| static atomic_int round_no = 0, winner = -1, ready = 0, done = 0, quit = 0; | |
| static int N, BLOCK; | |
| static void *worker(void *arg) { | |
| int id = (int)(intptr_t)arg, my_round = 0; | |
| for (;;) { | |
| while (atomic_load(&round_no) == my_round) // wait for the next round | |
| if (atomic_load(&quit)) return NULL; | |
| my_round = atomic_load(&round_no); | |
| atomic_fetch_add(&ready, 1); | |
| if (BLOCK) pthread_mutex_lock(&m); // sleep in the kernel until woken | |
| else while (pthread_mutex_trylock(&m) != 0) {} // busy-loop, as in the question | |
| int expected = -1; | |
| atomic_compare_exchange_strong(&winner, &expected, id); // first one in wins | |
| pthread_mutex_unlock(&m); | |
| atomic_fetch_add(&done, 1); | |
| } | |
| } | |
| int main(int argc, char **argv) { | |
| N = argc > 1 ? atoi(argv[1]) : 4; | |
| int R = argc > 2 ? atoi(argv[2]) : 20000; | |
| BLOCK = argc > 3 && !strcmp(argv[3], "block"); | |
| pthread_t t[64]; | |
| for (int i = 0; i < N; i++) pthread_create(&t[i], NULL, worker, (void *)(intptr_t)i); | |
| long count[64] = {0}, repeats = 0; | |
| int prev = -1; | |
| printf("first 60: "); | |
| for (int r = 0; r < R; r++) { | |
| pthread_mutex_lock(&m); // "thread 0" holds the lock | |
| atomic_store(&winner, -1); atomic_store(&ready, 0); atomic_store(&done, 0); | |
| atomic_fetch_add(&round_no, 1); | |
| while (atomic_load(&ready) < N) {} // everyone is now contending | |
| for (volatile int k = 0; k < 2000; k++) {} | |
| pthread_mutex_unlock(&m); // ...let go | |
| while (atomic_load(&done) < N) {} | |
| int w = atomic_load(&winner); | |
| count[w]++; repeats += (w == prev); prev = w; | |
| if (r < 60) printf("%d", w); | |
| } | |
| atomic_store(&quit, 1); | |
| for (int i = 0; i < N; i++) pthread_join(t[i], NULL); | |
| printf("\nmode=%s threads=%d rounds=%d\n", BLOCK ? "block" : "spin", N, R); | |
| double exp = (double)R / N, chi2 = 0; | |
| for (int i = 0; i < N; i++) { | |
| printf(" thread %d won %6ld (%5.1f%%)\n", i, count[i], 100.0 * count[i] / R); | |
| chi2 += (count[i] - exp) * (count[i] - exp) / exp; | |
| } | |
| printf(" same winner as previous round: %.1f%% (fair would be %.1f%%)\n", | |
| 100.0 * repeats / (R - 1), 100.0 / N); | |
| printf(" chi-square vs uniform: %.1f (fair coin-ish values are ~%d)\n", chi2, N - 1); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment