Commit 65bf7f3074b65f43c53b137b0a0988e5b01e3d63

Thomas de Grivel 2023-12-18T11:46:04

wip random

diff --git a/libc3/u.c.in b/libc3/u.c.in
index aa34b3d..f212f01 100644
--- a/libc3/u.c.in
+++ b/libc3/u.c.in
@@ -94,23 +94,17 @@ u_bits$ * u_bits$_random (u_bits$ *u)
 
 u_bits$ * u_bits$_random_uniform (u_bits$ *u, u_bits$ max)
 {
-  u16 bits;
-  u8 i;
   u_bits$ rest;
   u_bits$ result;
-  u8 words;
   assert(u);
   assert(max);
-  bits = log2l(max) - 1;
-  words = bits / 32;
+  rest = max;
   result = 0;
-  i = 0;
-  while (i < words) {
+  while (rest > (u32) -1) {
+    rest >>= 32;
     result <<= 32;
     result += arc4random();
-    i++;
   }
-  rest = max >> (words * 32);
   if (rest) {
     result <<= 32;
     result += arc4random_uniform(rest);
diff --git a/libc3/u16.c b/libc3/u16.c
index de2eb63..b518653 100644
--- a/libc3/u16.c
+++ b/libc3/u16.c
@@ -94,23 +94,17 @@ u16 * u16_random (u16 *u)
 
 u16 * u16_random_uniform (u16 *u, u16 max)
 {
-  u16 bits;
-  u8 i;
   u16 rest;
   u16 result;
-  u8 words;
   assert(u);
   assert(max);
-  bits = log2l(max) - 1;
-  words = bits / 32;
+  rest = max;
   result = 0;
-  i = 0;
-  while (i < words) {
+  while (rest > (u32) -1) {
+    rest >>= 32;
     result <<= 32;
     result += arc4random();
-    i++;
   }
-  rest = max >> (words * 32);
   if (rest) {
     result <<= 32;
     result += arc4random_uniform(rest);
diff --git a/libc3/u32.c b/libc3/u32.c
index 9e3f11e..7233179 100644
--- a/libc3/u32.c
+++ b/libc3/u32.c
@@ -94,23 +94,17 @@ u32 * u32_random (u32 *u)
 
 u32 * u32_random_uniform (u32 *u, u32 max)
 {
-  u16 bits;
-  u8 i;
   u32 rest;
   u32 result;
-  u8 words;
   assert(u);
   assert(max);
-  bits = log2l(max) - 1;
-  words = bits / 32;
+  rest = max;
   result = 0;
-  i = 0;
-  while (i < words) {
+  while (rest > (u32) -1) {
+    rest >>= 32;
     result <<= 32;
     result += arc4random();
-    i++;
   }
-  rest = max >> (words * 32);
   if (rest) {
     result <<= 32;
     result += arc4random_uniform(rest);
diff --git a/libc3/u64.c b/libc3/u64.c
index 9fdb100..cf2ef47 100644
--- a/libc3/u64.c
+++ b/libc3/u64.c
@@ -94,23 +94,17 @@ u64 * u64_random (u64 *u)
 
 u64 * u64_random_uniform (u64 *u, u64 max)
 {
-  u16 bits;
-  u8 i;
   u64 rest;
   u64 result;
-  u8 words;
   assert(u);
   assert(max);
-  bits = log2l(max) - 1;
-  words = bits / 32;
+  rest = max;
   result = 0;
-  i = 0;
-  while (i < words) {
+  while (rest > (u32) -1) {
+    rest >>= 32;
     result <<= 32;
     result += arc4random();
-    i++;
   }
-  rest = max >> (words * 32);
   if (rest) {
     result <<= 32;
     result += arc4random_uniform(rest);
diff --git a/libc3/u8.c b/libc3/u8.c
index b4652c9..f64bd2f 100644
--- a/libc3/u8.c
+++ b/libc3/u8.c
@@ -94,23 +94,17 @@ u8 * u8_random (u8 *u)
 
 u8 * u8_random_uniform (u8 *u, u8 max)
 {
-  u16 bits;
-  u8 i;
   u8 rest;
   u8 result;
-  u8 words;
   assert(u);
   assert(max);
-  bits = log2l(max) - 1;
-  words = bits / 32;
+  rest = max;
   result = 0;
-  i = 0;
-  while (i < words) {
+  while (rest > (u32) -1) {
+    rest >>= 32;
     result <<= 32;
     result += arc4random();
-    i++;
   }
-  rest = max >> (words * 32);
   if (rest) {
     result <<= 32;
     result += arc4random_uniform(rest);
diff --git a/libc3/uw.c b/libc3/uw.c
index f1d7795..dd77b80 100644
--- a/libc3/uw.c
+++ b/libc3/uw.c
@@ -94,23 +94,17 @@ uw * uw_random (uw *u)
 
 uw * uw_random_uniform (uw *u, uw max)
 {
-  u16 bits;
-  u8 i;
   uw rest;
   uw result;
-  u8 words;
   assert(u);
   assert(max);
-  bits = log2l(max) - 1;
-  words = bits / 32;
+  rest = max;
   result = 0;
-  i = 0;
-  while (i < words) {
+  while (rest > (u32) -1) {
+    rest >>= 32;
     result <<= 32;
     result += arc4random();
-    i++;
   }
-  rest = max >> (words * 32);
   if (rest) {
     result <<= 32;
     result += arc4random_uniform(rest);