Restore constant pseudo-randomness in PHP 5.2

I include the same "random.inc" in foo.php and bar.php. For everyone, I want reproducible “random” results.

So, in foo.php, I always need one set of numbers and / or keywords. In bar.php is different. Which should not change upon reboot. This is what I mean by the alias contant . And that’s why I sow by URL. However, I still get different results for single numbers, as well as for pickson array for every reboot. This is the complete php file:

<?
    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'=>21,'1'=>89,'2'=>96,'3'=>47,'4'=>88 );
    print_r( array_rand( $values, 3 ) );
?>

In the days of PHP4.1, it was (verified) possible to achieve a constant pseudo-random type. the documentation for the array array_rand describes as a function that, since the initialization of 4.2, occurs automatically. Perhaps this is a redefinition of any explicit seeding? (if so, maybe explicit sowing should raise the internal PHP flag, preventing automatic sowing?). Btw: mt_srand () and srand () do not work equally.

I would love to get my deterministic / constant pseudo-random inverse ...

Update: Solution below (Windows error and / or version 5.2)

+3
source share
5 answers

You guys are all right! (Sorry, I answer it myself now)

  • - 5.2.17 Linux 2.6.36, .
  • Win x64 5.3.0 , .

, , , / PHP, ​​ 5.3.0.

, , , 5.3 , 4.2. , , , .

+2

(PHP/5.3.6):

<?php

$data = range(1, 100);
srand(1);
print_r(array_rand($data, 3));

... :

Array
(
    [0] => 21
    [1] => 89
    [2] => 95
)

... . -, , .

+3

; , PHP 4.2 ; reset .

[] , , !

mt_srand(50000);
print "rand="+mt_rand(0,10000);

PHP 5.2, , (1749).

[EDIT] @cwd, , -, PHP 5.2 Linux Windows. PHP 5.2 Linux .

, , -, ​​ PHP 5.3, . (PHP 5.2 , )

+2

Btw, - " windows-pre-5.3 " ( , , , buzzwording SEO), :

$r = abs(crc32($URL))%20; // a number between 0 and 19, based on URL
+1

PHP 5.2.17 , , PHP 5.2 ( ) , PHP , .

rand mt_rand "" , , - !

PHP , "", , , PHP 5.3.

php mt_rand .

+1

All Articles