shithub: util

Download patch

ref: 472b27702ebbd4aec6c48f2c44a471835dbfbf52
parent: d9fb5fea1930686c2f70914bbdabbe5f1970990a
author: eli <eli@singularity>
date: Wed Oct 1 02:14:44 EDT 2025

llama2 /dev/random

--- a/llama2.c
+++ b/llama2.c
@@ -1070,11 +1070,24 @@
 }
 
 unsigned int random_u32(unsigned long long *state) {
-    // xorshift rng: https://en.wikipedia.org/wiki/Xorshift#xorshift.2A
-    *state ^= *state >> 12;
-    *state ^= *state << 25;
-    *state ^= *state >> 27;
-    return (*state * 0x2545F4914F6CDD1Dull) >> 32;
+	unsigned int r;
+	int fd;
+
+	fd = open("/dev/random", OREAD);
+	if (fd == -1) {
+		fprint(2, "open random failed\n");
+
+	    // xorshift rng: https://en.wikipedia.org/wiki/Xorshift#xorshift.2A
+	    *state ^= *state >> 12;
+	    *state ^= *state << 25;
+	    *state ^= *state >> 27;
+	    return (*state * 0x2545F4914F6CDD1Dull) >> 32;
+	}
+
+	read(fd, &r, sizeof(unsigned int));
+	close(fd);
+
+	return r;
 }
 float random_f32(unsigned long long *state) { // random float32 in [0,1)
     return (random_u32(state) >> 8) / 16777216.0f;
--