user3201068
user3201068

Reputation: 147

What are the maximum size for a seed in php?

What is the maximum input value for a seed in PHP? Is it 2^32? 2^64? No limit?

I cannot seem to find an answer in PHP documentation.

Upvotes: 0

Views: 277

Answers (3)

user2864740
user2864740

Reputation: 61975

php_srand takes a long, and it calls one of srandom, srand48, or srand (depending upon compilation defines).

It casts the seed value to an int in the case of srandom/srand, but passes the full long-typed value through to srand48 - so then it comes down to implementation of the system/stdlib random functions (and actual size of int).


Also, considering that mt_srand takes a uint32, I'd say it's reliable to expect 32-bit seeds to be considered distinct (even if they start the same sequence). A 64-bit seed is the upper value limit (even if the entire seed is not used), but is only realizable in a 64-bit version of PHP.

Any non-integer value (i.e. float) supplied as a seed will be coerced to an integer. The actual php_srand C function is only called after coercing the supplied PHP value to a long in the wrapper.

Upvotes: 4

Daniel A. White
Daniel A. White

Reputation: 191048

srand takes an int which I assume is a 32-bit number on 32-bit machines according to the docs. and 64 on 64-bit machines.

Upvotes: 0

hrbrmstr
hrbrmstr

Reputation: 78832

You can use getrandmax() to determine the maximum value. ref: http://www.php.net/manual/en/function.getrandmax.php

Upvotes: 1

Related Questions