GenerateData - Make the generated sample data more stable (wrt mt_rand)
authorTim Otten <totten@civicrm.org>
Wed, 16 Sep 2020 07:30:56 +0000 (00:30 -0700)
committerTim Otten <totten@civicrm.org>
Wed, 16 Sep 2020 10:37:08 +0000 (03:37 -0700)
CRM/Core/CodeGen/GenerateData.php

index 8ad5fdedf66c287dc575406e04eeb1364fc6022c..74e0a4d41a9d5f526defe058eb8a75def3d0d7cc 100644 (file)
@@ -27,8 +27,9 @@ class CRM_Core_CodeGen_GenerateData {
   /**
    * Class constructor
    */
-  public function __construct() {
+  public function __construct($seed = '123456789') {
     // initialize all the vars
+    $this->seed = $seed;
     $this->numIndividual = self::INDIVIDUAL_PERCENT * self::NUM_CONTACT / 100;
     $this->numHousehold = self::HOUSEHOLD_PERCENT * self::NUM_CONTACT / 100;
     $this->numOrganization = self::ORGANIZATION_PERCENT * self::NUM_CONTACT / 100;
@@ -104,6 +105,11 @@ class CRM_Core_CodeGen_GenerateData {
    *
    */
 
+  /**
+   * @var int
+   */
+  private $seed;
+
   /**
    * enum's from database
    * @var array
@@ -201,12 +207,17 @@ class CRM_Core_CodeGen_GenerateData {
    *
    * All other random() functions should derive from this.
    *
+   * This is very weak RNG. The goal is to provide a reproducible sequence of
+   * random-ish values for generating dummy-data.
+   *
    * @param int $min
    * @param int $max
    * @return int
    */
   private function randomInt($min, $max) {
-    return mt_rand($min, $max);
+    $range = min(1 + $max - $min, mt_getrandmax());
+    $this->seed = md5($this->seed . chr(0) . $min . chr(0) . $max);
+    return $min + (hexdec(substr($this->seed, 20, 8)) % $range);
   }
 
   /**