Note that there are some explanatory texts on larger screens.

plurals
  1. POregain constant pseudo-random in PHP 5.2
    primarykey
    data
    text
    <p>I am including the same "random.inc" in foo.php and bar.php. For each, I want reproducible "random" results. </p> <p>So in foo.php I always want one set of numbers and/or keywords. In bar.php another. Which shouldn't change on reload. That's what I mean by <strong>contant</strong> pseudo-random. And that's why I seeding on the url. However I still get different results for individual numbers as well as for array pickson every reload. This is the full php file:</p> <pre><code>&lt;? header('Content-Type: text/plain'); $seed = crc32( $_SERVER['REQUEST_URI'] ); echo "phpversion: ".phpversion()."\nseed: $seed\n"; srand( $seed ); // (seed verified to be contant as expected) // neither single values nor array pics turn out deterministic echo ''.rand(0,100).' '.rand(0,100).' '.rand(0,100)."\n"; $values = array( '0'=&gt;21,'1'=&gt;89,'2'=&gt;96,'3'=&gt;47,'4'=&gt;88 ); print_r( array_rand( $values, 3 ) ); ?&gt; </code></pre> <p>In the days of PHP4.1 it was (verified) possible to achieve constant pseudo-random like this. <a href="http://php.net/manual/en/function.array-rand.php" rel="nofollow">array_rand API documentation</a> describes as a feature that since 4.2 initialization happens automatically. Perhaps this is overriding any explicit seeding? (if so, perhaps explicit seeding should raise an internal PHP flag, preventing automatic seeding?). Btw: mt_srand() and srand() are equally not working.</p> <p>I would really like to get my <em>deterministic</em> / <em>constant</em> pseudo-random back...</p> <p><strong>Update:</strong> Solution below (Windows and/or version 5.2 's fault)</p>
    singulars
    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.
 

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