Remove use of mt_srand and mt_rand from yioop hash_table, a=chris

Chris Pollett [2013-04-13 06:Apr:th]
Remove use of mt_srand and mt_rand from yioop hash_table, a=chris
Filename
lib/hash_table.php
lib/utility.php
tests/hash_table_test.php
diff --git a/lib/hash_table.php b/lib/hash_table.php
index 4f1e7e167..1569b9888 100755
--- a/lib/hash_table.php
+++ b/lib/hash_table.php
@@ -318,12 +318,9 @@ class HashTable extends StringArray
      */
     function hash($key)
     {
-        $hash = substr(md5($key, true), 0, 4);
-        $seed = unpackInt($hash);
-
-        mt_srand($seed);
-        $index = mt_rand(0, $this->num_values - 1);
-
+        $hash = substr(md5($key, true), 0, 3);
+        $pre_index = unpackInt($hash);
+        $index = floor($pre_index * $this->num_values/(2 << 23));
         return $index;
     }

diff --git a/lib/utility.php b/lib/utility.php
index d35ccf9f5..96bccb481 100755
--- a/lib/utility.php
+++ b/lib/utility.php
@@ -1082,6 +1082,4 @@ function general_is_a($class_1, $class_2)
     if($class_1 == $class_2) return true;
     return (is_a($class_1, $class_2) || is_subclass_of($class_1, $class_2));
 }
-
-
 ?>
diff --git a/tests/hash_table_test.php b/tests/hash_table_test.php
index 70afb8367..77144f79c 100755
--- a/tests/hash_table_test.php
+++ b/tests/hash_table_test.php
@@ -187,18 +187,18 @@ class HashTableTest extends UnitTest
             $index, $index2, "Index of reinserted should not change");

         $this->assertTrue(
-            $this->test_objects['FILE2']->insert(crawlHash("hi4",true), "8"),
-            "Item hi4 which collides with hi7 insert okay");
+            $this->test_objects['FILE2']->insert(crawlHash("hi11",true), "8"),
+            "Item hi11 which collides with hi7 insert okay");
         $this->assertTrue(
             $this->test_objects['FILE2']->lookup(
-                crawlHash("hi4",true), HashTable::ALWAYS_RETURN_PROBE),
-                $index2 + 1, "Item hi4 located one after hi7");
+                crawlHash("hi11",true), HashTable::ALWAYS_RETURN_PROBE),
+                $index2 + 1, "Item hi11 located one after hi7");
         $this->test_objects['FILE2']->delete(crawlHash("hi7",true));
         $this->assertTrue(
             $this->test_objects['FILE2']->lookup(
-                crawlHash("hi4",true), true), $index2 + 1,
-            "Item hi4 looked up succeed after hi7 deleted");
-        $this->test_objects['FILE2']->delete(crawlHash("hi4",true));
+                crawlHash("hi11",true), true), $index2 + 1,
+            "Item hi11 looked up succeed after hi7 deleted");
+        $this->test_objects['FILE2']->delete(crawlHash("hi11",true));
         $this->test_objects['FILE2']->insert(crawlHash("hi7",true), "7");
         $this->assertEqual(
             $this->test_objects['FILE2']->lookup(
ViewGit