|
użytkowników online: 26
|
|
OPINIE UŻYTKOWNIKÓW
|
O wysokich kompetencjach zawodowych Darka nie ma co dyskutować. Wszyscy chyba jesteśmy zgodni co do tego, że Jego wiedza na polu informatycznym jest bogata i zasługuje na uznanie. Swego czasu zwróciłem się z prośbą o pomoc w realizacji małego projektu internetowego. Projekt był niewielki, jednak jego realizacja wymagała pewnego doświadczenia. Darek podjął się tego zlecenia, wykonał je szybko i sprawnie. Podczas realizacji służył doradztwem, jednak w żaden sposób nie narzucał swojego zdania. O prawidłowości Jego koncepcji przekonałem się dopiero po pewnym czasie. To, czego ja nie dostrzegałem, On dostrzegał i zwracał na to moją uwagę. Darek dał się poznać nie tylko, jako dobry fachowiec, co przede wszystkim okazał się być rzetelnym i uczciwym kontrahentem. Tak więc nie dość, że fachowiec, to jeszcze uczciwy. Polecam usługi Darka wszystkim tym, którzy szukają fachowej pomocy przy realizacji nawet najbardziej złożonych projektów.
Dariusz Żwan
Actuarius.pl
|
|
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net
[Spis]
[A]
[B]
[C]
[D]
[E]
[F]
[G]
[H]
[I]
[J]
[K]
[L]
[M]
[N]
[O]
[P]
[Q]
[R]
[S]
[T]
[U]
[V]
[X]
[W]
[Z]
mt_srand (PHP 3 >= 3.0.6, PHP 4, PHP 5) mt_srand -- Inicjuje generator Mersenne Twister Opisvoid mt_srand ( int ziarno )
Inicjuje generator liczb losowych za pomocą ziarna
ziarno.
Notatka:
Począwszy od PHP 4.2.0, nie trzeba już inicjować generatora liczb
losowych przed użyciem.
Patrz także: mt_rand(),
mt_getrandmax(), srand().
User Contributed Notesgigs
12-Jan-2006 08:13
used the little script from mrcheezy at hotmail dot com and got much better results using
mt_srand(crc32(microtime()));
vbassassin at coderheaven dot com
01-Dec-2005 05:57
"Better still: Use a 31-bit hash of microtime() as the seed. "
Correct me if i am wrong, but woudlnt using microtime() still limit the total seeds to 1,000,000 again? Since the 31-bit hash will always give the same hash for the same number, and in the microtime() function you could have 1,000,000 or less numbers. So in effect your still no better off at all :-p
Best regards,
scott
PS: I actually agree that PHP has pretty much resolved the issue and got as close as anyones going to get to solving the seeding issue by introducing the "Mersenne Twister" algorithm which creates a much larger pool than 1,000,000 numbers. Just because the mt_srand() function exists doesnt mean you HAVE to use it ;-) use it if you NEED a specific list of the same numbers (comes in handy for encryptions with passwords ;-)
12-Sep-2005 03:03
It's better to use the following method instead of the one in the documentation metioned:
<?php
mt_srand((double)(microtime() ^ posix_getpid()));
?>
Otherwise people requesting the script at the same time could get the same generated number.
Schrecktech
21-Jan-2005 03:16
::: My seeding report :::
For FreeBSD 4.8-STABLE PHP 4.3.10 it was good enough and faster to not seed at all...although I did not test the random pseudo terminal.
In detail only seeding once by a previous seed using the hex version (case 3 below) for larger numbers and longer run times faired the best. For small numbers it was better to not seed at all (let alone faster). Play with the $loop and $max numbers to see...
<?
function get_a_good_seed
(
$choice = 3
)
{
static $seed;
$seed = 0;
switch($choice)
{
case 1:
list($usec, $sec) = explode(' ', microtime());
$seed = (float) $sec + ((float) $usec * 1000000);
break;
case 2:
$seed = (double)microtime()*1000003;
break;
case 3:
$seed = hexdec(substr(md5(microtime()), -8)) & 0x7fffffff;
break;
case 4:
break;
default:
break;
}
return($seed);
}
function load_last_random
(
)
{
static $last;
static $handle;
$handle = fopen("random.txt", "r");
while (!feof($handle)) {
$last = fgets($handle, 4096);
}
fclose($handle);
return($last);
}
function save_last_random
(
$save = 0
)
{
$handle = fopen("random.txt", "w");
fwrite($handle, $save);
fclose($handle);
}
function seed_random_number_generator
(
$type = 3,
$ignore_generated_already = FALSE
)
{
static $generator_seeded = FALSE;
if($ignore_generated_already || !$generator_seeded)
{
mt_srand(( load_last_random()+get_a_good_seed($type) ) & 0x7fffffff);
save_last_random(mt_rand());
$generator_seeded= TRUE;
}
}
?>
fasaxc at yahoo dot co dot uk
25-Jul-2003 05:01
In fact, here's an even better function than the one below assuming your install provides a random entropy daemon and you're running *nix (to check for the former type "head -c 6 /dev/urandom" on the command line if available - if you get 6 random characters you're set). N.B. php must be able to find the head program so it must be in your path and allowed if you're running safe mode.
The functions db_set_global() and db_get_global() I use to set/get a variable from a central database but you could save/restore the variable from a file instead or just use the function get_random_word().
<?
function get_random_word($force_random=False) {
if ($force_random) {
$u='';
} else {
$u='u';
}
$ran_string=shell_exec("head -c 4 /dev/{$u}random");
$random=ord(substr($ran_string,0,1))<<24 |
ord(substr($ran_string,1,1))<<16 |
ord(substr($ran_string,2,1))<<8 |
ord(substr($ran_string,3,1));
return $random;
}
--EITHER - IF YOU'VE SET UP A DATABASE OF GLOBAL VARIABLES--
## If the seed is found in the database
if ($seed=db_get_global('seed')) {
# use mt_rand() to get the next seed
mt_srand($seed);
# then XOR that with a random word
$seed=(mt_rand() ^ get_random_word());
} else {
## Make a completely new seed (First Run)
# Generate the seed as a proper random no using /dev/random
$seed=get_random_word(True);
mt_srand($seed);
}
db_set_global('seed',$seed);
--OR JUST--
mt_srand(get_random_word());
?>
fasaxc at yahoo dot com
18-Jul-2003 01:14
The best way to ensure a random seed is to do the following:
To start:
1) get your initial seed with mt_srand(microtime() * 1000000)
2) generate a random no. $random=mt_rand()
3) save this number in a file (or database or whatever so that it is available next time the page is loaded)
Now, for each time your script is loaded :
1) load the value you saved above and do $new_seed=($random+(microtime() * 1000000))%pow(2,32)
2) mt_srand($new_seed);
3) generate a new random no. $random=mt_rand()
4) save that number back in the file/database
This procedure takes advantage not only of the randomness of microtime() but of all the previous calls to microtime() so your seed becomes better and better with time. It also generates good seeds even on platforms where microtime() doesn't take all the values it can.
Just using microtime() * 1000000 only results in 1000000 possible seeds (and less on some platforms as noted) - the function above gives 2^32 seeds with an avelanche effect accross multiple executions.
mrcheezy at hotmail dot com
17-Feb-2003 11:14
Very good points above on seeds, thank you. If you would like to test a seed try using the code below. It will take between 5 and 20 seconds depending on your system and then will spit out the number of reused keys out of 100,000 attempts.
; for ($i=0; $i<100000; $i++) {
; mt_srand(hexdec(substr(md5(microtime()), -8)) & 0x7fffffff);
; $rand = mt_rand();
;
; ($arr[$rand] == '1') ? $k++ : $arr[$rand] = '1';
; }
mattb at columbia dot edu
12-Dec-2002 09:54
This is effectively the same thing, but it uses a more efficient bit mask instead of a substring. Here's the verbose version:
function mkseed()
{
$hash = md5(microtime());
$loWord = substr($hash, -8);
$seed = hexdec($loWord);
$seed &= 0x7fffffff;
return $seed;
}
Or, if you're a real hacker, this is a more concise version:
function mkseed()
{
return hexdec(substr(md5(microtime()), -8)) & 0x7fffffff;
}
Note, the range on the md5 sum is really arbitrary. You could use whatever portion you like, just so long as it's 32 bits.
Now just call mt_srand() (once) as follows:
mt_srand(mkseed());
Of course, unless you wanted the mkseed() function for something else, you could skip all that jive and just do this somewhere in a global include file:
<?php
if (!isset($_MyApp_MtSrand)
|| !$_MyApp_MtSrand)
{
$_MyApp_MtSrand = true;
mt_srand(hexdec(substr(md5(microtime()), -8)) & 0x7fffffff);
}
?>
ghaecker_no_spam at idworld dot net
21-Sep-2002 10:20
The range of unique seeds using this method is a bit over 2 billion. This approach also prevents re-seeding.
function seed_mt_rand() {
|