note 81379 modified in function.rand by danbrown

From: Date: Wed, 05 Nov 2008 15:37:24 +0000
Subject: note 81379 modified in function.rand by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-146171@lists.php.net to get a copy of this message
Using the script below with rand() instead of mt_rand(), I was suprised to find many "words" and part of "phrases" in common. mt_rand() is highly recomanded when you want to build test cases. <?php $ressbdd = mysql_connect("localhost", "usr", "pass") or die("Could not connect: " . mysql_error()); for($i=0;$i<100000;$i++){ $xx1=mt_rand(15,80); $texte = phrase($xx1); $sql = 'INSERT into phrases.valeurs set valeur = \'' . $texte . '\''; mysql_query( $sql , $ressbdd ); } //============================================= function phrase($n){ $phrase=''; for($i=0;$i<$n;$i++){ $xx2=mt_rand(2,20); $phrase.=mot($xx2) . ' '; } return(substr($phrase,0,-1).'.'); // phrase ends with a . } //============================================= function mot($n){ if($n<=0) return 'hello'; if($n>=31) return 'world'; $voyelle =Array('a','e','i','o','u','y'); $consonne=Array( 'b','c','d','f','g','h','j','k','l','m', 'n','p','q','r','s','t','v','w','x','z'); $mot=''; for($i=0;$i<$n;$i++){ if($i%2==0){ $xx3=mt_rand(0,19); $mot.=$consonne[$xx3]; }else{ $xx4=mt_rand(0,5); $mot.=$voyelle[$xx4]; } } return $mot; } ?> --was-- Using the script below with rand() instead of mt_rand(), I was suprised to find many "words" and part of "phrases" in common. mt_rand() is highly recomanded when you want to build test cases. $ressbdd = mysql_connect("localhost", "usr", "pass") or die("Could not connect: " . mysql_error()); for($i=0;$i<100000;$i++){ $xx1=mt_rand(15,80); $texte = phrase($xx1); $sql = 'INSERT into phrases.valeurs set valeur = \'' . $texte . '\''; mysql_query( $sql , $ressbdd ); } //============================================= function phrase($n){ $phrase=''; for($i=0;$i<$n;$i++){ $xx2=mt_rand(2,20); $phrase.=mot($xx2) . ' '; } return(substr($phrase,0,-1).'.'); // phrase ends with a . } //============================================= function mot($n){ if($n<=0) return 'hello'; if($n>=31) return 'world'; $voyelle =Array('a','e','i','o','u','y'); $consonne=Array( 'b','c','d','f','g','h','j','k','l','m', 'n','p','q','r','s','t','v','w','x','z'); $mot=''; for($i=0;$i<$n;$i++){ if($i%2==0){ $xx3=mt_rand(0,19); $mot.=$consonne[$xx3]; }else{ $xx4=mt_rand(0,5); $mot.=$voyelle[$xx4]; } } return $mot; } http://php.net/manual/en/function.rand.php

« previous php.notes (#146171) next »