]> git.ipfire.org Git - thirdparty/plymouth.git/commitdiff
ply-utils: Ensure random ints are big enough
authoremperor06 <github@draxar.com>
Tue, 4 Jun 2024 14:42:11 +0000 (16:42 +0200)
committerRay Strode <rstrode@redhat.com>
Tue, 4 Jun 2024 15:05:05 +0000 (11:05 -0400)
Using Math.Random() in a theme script practically always returns zero.
This is because ply_get_random_number uses mrand48 which, while
returning a 64-bit long, restricts the range of its return value
to be no more than 32-bit, and so gets improperly normalized.

This commit addresses the problem by calling mrand48() twice, once for
each 32-bits of the returned value.

https://gitlab.freedesktop.org/plymouth/plymouth/-/issues/256

src/libply/ply-utils.c

index 44a930994a461831863a932e36789520494fa046..0e317b6783d626058ad9722e732b6291f5df030a 100644 (file)
@@ -1308,7 +1308,7 @@ ply_get_random_number (long lower_bound,
                 seed_initialized = true;
         }
 
-        offset = mrand48 ();
+        offset = (mrand48 () << 32) | (mrand48 () & 0xffffffff);
 
         offset = labs (offset) % range;