about summary refs log tree commit diff
path: root/lib/util
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2021-12-18 22:38:46 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2021-12-18 22:38:46 +0000
commitc120160d6eaaa35adad81942a2d69e52e8600945 (patch)
treea86996b19a63a0192b0803b8c44b959133742767 /lib/util
parent51f0e58c7c897559aceb42f2abd0f1be28bbdf03 (diff)
downloadnetpbm-mirror-c120160d6eaaa35adad81942a2d69e52e8600945.tar.gz
netpbm-mirror-c120160d6eaaa35adad81942a2d69e52e8600945.tar.xz
netpbm-mirror-c120160d6eaaa35adad81942a2d69e52e8600945.zip
Add pm_rand32
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@4205 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'lib/util')
-rw-r--r--lib/util/rand.c43
-rw-r--r--lib/util/rand.h5
2 files changed, 43 insertions, 5 deletions
diff --git a/lib/util/rand.c b/lib/util/rand.c
index 2f60de83..6a0a2cdb 100644
--- a/lib/util/rand.c
+++ b/lib/util/rand.c
@@ -72,8 +72,8 @@ https://wnww.gnu.org/software/gsl/doc/html/rng.html
   Twister method and does not rely on any randomness facility of the operating
   system, but it is easy to compile an alternative version that uses others.
 
-  The Mersenne Twister method was new to Netpbm in Netpbm 10.94
-  (March 2021).  Before that, Netpbm used standard OS-provided facilities.
+  The Mersenne Twister method was new to Netpbm in Netpbm 10.94 (March 2021).
+  Before that, Netpbm used standard OS-provided facilities.
 
   Programs that use random numbers have existed in Netpbm since PBMPlus days.
   The system rand() function was used in instances randomness was required;
@@ -87,15 +87,15 @@ https://wnww.gnu.org/software/gsl/doc/html/rng.html
 
   This was not considered a problem in the early days.  Deterministic
   operation was not a feature users requested and it was impossible regardless
-  of the random number generation method on most programs because they did
-  not allow a user to specify a seed for the generator.
+  of the random number generation method on most programs because they did not
+  allow a user to specify a seed for the generator.
 
   This state of affairs changed as Netpbm got firmly established as a
   base-level system package.  Security became critical for many users.  A
   crucial component of quality control is automated regression tests (="make
   check").  Unpredictable behavior gets in the way of testing.  One by one
   programs were given the -randomseed (or -seed) option to ensure reproducible
-  results.  Often this was done as new tests cases were written.  However,
+  results.  Often this was done as new test cases were written.  However,
   inconsistent output caused by system-level differences in rand()
   implementation remained a major obstacle.
 
@@ -219,6 +219,39 @@ pm_gaussrand(struct pm_randSt * const randStP) {
 
 
 
+uint32_t
+pm_rand32(struct pm_randSt * const randStP) {
+/*-----------------------------------------------------------------------------
+  Generate a 32-bit random number.
+
+  This is a provision for users who select a non-default random number
+  generator which returns less than 32 bits per call.  Many system generators
+  are known to return 31 bits (max = 2147483647 or 0x7FFFFFFF).
+
+  This does not work with generators that return less than 11 bits per call.
+  The least we know of is the archaic RANDU, which generates 15 bits (max =
+  32767 or 0x7FFF).
+-----------------------------------------------------------------------------*/
+    unsigned int const randMax = randStP->max;
+
+    uint32_t retval;
+
+    if (randMax >= 0xFFFFFFFF)
+        retval = pm_rand(randStP);
+    else {
+        uint32_t scale;
+
+        retval = 0;  /* Initial value */
+
+        for (scale = 0xFFFFFFFF; scale > 0; scale /= (randMax +1))
+            retval *= (randMax + 1) + pm_rand(randStP);
+    }
+
+    return retval;;
+}
+
+
+
 void
 pm_randinit(struct pm_randSt * const randStP) {
 /*----------------------------------------------------------------------------
diff --git a/lib/util/rand.h b/lib/util/rand.h
index c441890a..44095243 100644
--- a/lib/util/rand.h
+++ b/lib/util/rand.h
@@ -3,6 +3,8 @@
 #ifndef RAND_H_INCLUDED
 #define RAND_H_INCLUDED
 
+#include <inttypes.h>
+
 #include "netpbm/pm_c_util.h"
 #include "netpbm/mallocvar.h"
 
@@ -103,5 +105,8 @@ pm_gaussrand2(struct pm_randSt * const randStP,
 extern double
 pm_gaussrand(struct pm_randSt * const randStP);
 
+extern uint32_t
+pm_rand32(struct pm_randSt * const randStP);
+
 
 #endif