Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using some basic maths and a few tricks you can quite easily create your OWN random function like i have just done :)</p> <p>Sorry i haven't cleaned it up. It would be much better in a class as you could prevent the need to keep re-seeding it with the previous seed. Don't use a static variable as it limits you to only using 1 seed at a time (or manually keeping track of the seeds yourself). OOP would solve that. Do what you like with the function below, but let me know if you rewrite it.</p> <pre><code>/** * returns a decimal between 0 and max_number, requires seeding every time and will ALWAYS return the same decimal for the same seed * @copyright scott thompson, all rights reserved * @license MIT (do what you like with this) * @param string $seed * @param int $max_number=100 adjust the maximum number range */ function random_number($seed, $max_number = 100) { //make sure there won't be any deadspace where random numbers will never fill if ($max_number &gt; 0xFFFFFF) { trigger_error("Max random number was to high. Maximum number of " . 0xFFFFFF . " allowed. Defaulting to maximum number.", E_USER_WARNING); $max_number = 0xFFFFFF; } //hash the seed to ensure enough random(ish) characters each time $hash = sha1($seed); //use the first x characters, and convert from hex to base 10 (this is where the random number is obtain) $rand = base_convert(substr($hash, 0, 6), 16, 10); //as a decimal percentage (ensures between 0 and max number) return $rand / 0xFFFFFF * $max_number ; } $seed = 'hello'; print ($seed = random_number($seed)) . '&lt;br /&gt;'; //66.779748605475 print ($seed = random_number($seed)) . '&lt;br /&gt;'; //3.5753311857779 print ($seed = random_number($seed)) . '&lt;br /&gt;'; //13.994396567011 print ($seed = random_number($seed)) . '&lt;br /&gt;'; //70.344917198713 print ($seed = random_number($seed)) . '&lt;br /&gt;'; //4.5583250855401 </code></pre> <p>Hope this helps, scott</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload