By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,521 Members | 1,420 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,521 IT Pros & Developers. It's quick & easy.

PHP mt_srand no longer seeds same number!!!!! aaarrrg

P: n/a
A
Hi all.

Is this a bug or what???

here is a simple code:

<?php
mt_srand(1);
echo mt_rand(0, 255)."<br />";
echo mt_rand(0, 255)."<br />";
echo mt_rand(0, 255)."<br />";
echo mt_rand(0, 255)."<br />";
?>

It should produce the SAME sequence every time when run.

however... it does not on PHP 5.2.6 on the linux box.

But on my test environment (windows) - php 5.2.3 does work properly!

So what am I missing? How do I get the same random sequences based on
mt_rand and mt_srand? I need same sequences based on starting seed!

I looked at the docs and it says:

The Mersenne Twister implementation in PHP now uses a new seeding
algorithm by Richard Wagner.
Identical seeds no longer produce the same sequence of values they did
in previous versions.
This behavior is not expected to change again, but it is considered
unsafe to rely upon it nonetheless.
but it says - AFTER 5.2.1 --- and I have both versions ABOVE 5.2.1...what to
do? ideas? pleeeeeeaseeeeeeee! :)
Sep 12 '08 #1
Share this Question
Share on Google+
20 Replies


P: n/a
A wrote:
Hi all.

Is this a bug or what???

here is a simple code:

<?php
mt_srand(1);
echo mt_rand(0, 255)."<br />";
echo mt_rand(0, 255)."<br />";
echo mt_rand(0, 255)."<br />";
echo mt_rand(0, 255)."<br />";
?>

It should produce the SAME sequence every time when run.

however... it does not on PHP 5.2.6 on the linux box.

But on my test environment (windows) - php 5.2.3 does work properly!

So what am I missing? How do I get the same random sequences based on
mt_rand and mt_srand? I need same sequences based on starting seed!

I looked at the docs and it says:

The Mersenne Twister implementation in PHP now uses a new seeding
algorithm by Richard Wagner.
Identical seeds no longer produce the same sequence of values they did
in previous versions.
This behavior is not expected to change again, but it is considered
unsafe to rely upon it nonetheless.
but it says - AFTER 5.2.1 --- and I have both versions ABOVE 5.2.1...what to
do? ideas? pleeeeeeaseeeeeeee! :)
I don't know about 5.2.3, but it is working as designed in 5.2.6.

Random numbers are exactly that - random (actually, in programming they
can only be pseudo-random). Your problem is you are depending on the
ability to repeat a sequence. That's not a good assumption to make in
any language.

If you need to be able to repeat the sequence, I would suggest you write
your own pseudo-random number generator. That way you can control its
actions. They aren't too hard, and googling should get you several
examples.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Sep 12 '08 #2

P: n/a
A
just discovered that srand and rand behave the same way!

on 5.2.3 they produce identical seq...

on 5.2.6 always different doesn't care about the seed

dunno what to do. how to replace this? is this a php 5.2.6 bug? or a
feature?
Sep 12 '08 #3

P: n/a
A
Random numbers are exactly that - random (actually, in programming they
can only be pseudo-random). Your problem is you are depending on the
ability to repeat a sequence. That's not a good assumption to make in any
language.

If you need to be able to repeat the sequence, I would suggest you write
your own pseudo-random number generator. That way you can control its
actions. They aren't too hard, and googling should get you several
examples.
true but that just bloats the code for something that should work. seed
should start from the same point. yes, i need random numbers but with the
same starting point - that's the whole idea. thanks for the answer though.

any other ideas?
Sep 12 '08 #4

P: n/a
A wrote:
>Random numbers are exactly that - random (actually, in programming they
can only be pseudo-random). Your problem is you are depending on the
ability to repeat a sequence. That's not a good assumption to make in any
language.

If you need to be able to repeat the sequence, I would suggest you write
your own pseudo-random number generator. That way you can control its
actions. They aren't too hard, and googling should get you several
examples.

true but that just bloats the code for something that should work. seed
should start from the same point. yes, i need random numbers but with the
same starting point - that's the whole idea. thanks for the answer though.

any other ideas?
It does work. And no, a seed should not start from the same point. Such
an operation would not be even pseudo-random.

What you need is not random numbers - you need a sequence, which is
something entirely different.

Zend finally corrected the code, and you got caught because you depended
on something you shouldn't have.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Sep 12 '08 #5

P: n/a
..oO(Jerry Stuckle)
>I don't know about 5.2.3, but it is working as designed in 5.2.6.

Random numbers are exactly that - random (actually, in programming they
can only be pseudo-random). Your problem is you are depending on the
ability to repeat a sequence. That's not a good assumption to make in
any language.
Many languages allow to seed the random number generator, simply because
there are cases where you have to repeat the exact same sequence. You
still get random values, maybe even based on a random seed, but you're
able to reproduce them if necessary. Nothing wrong with that.

If there's such a seed function, then this is expected and documented
behaviour. With PHP's current MT implementation the seed function is
totally useless and the generator not backwards-compatible. I consider
this a bug or at least a really stupid design decision. If they would at
least have made this new behaviour optional.

rand()/srand() still seem to work correctly here (5.2.6):

<?php
srand(42);
for ($i = 3; $i--;) var_dump(rand());
srand(42);
for ($i = 3; $i--;) var_dump(rand());
?>

int(71876166)
int(708592740)
int(1483128881)
int(71876166)
int(708592740)
int(1483128881)

Micha
Sep 12 '08 #6

P: n/a
A
thanks for all your replies fellas.

i have an idea that's good enough for my purposes - I'll simply use a long
predefined random sequence and start from fixed "seed" point to get always
equal random sequence.

i also though i could use md5 for similar purpose as it also looks very much
random.

however, i do consider srand behaviour odd or a bug. as pseudo-random
numbers - they should behave within a certain pattern - the only thing is
that srand which should seed the random number with specified number should
start from the same point but as it seems it seeds it with random number. as
Michael Fesser says - pseudorandom function is called pseudorandom because
it generates same sequences for the given seed but this is not the case
here.

anyway, problem solved, thanks again but if you have some other ideas, i'll
be checking this post for a while as well.
Sep 12 '08 #7

P: n/a
..oO(A)
>however, i do consider srand behaviour odd or a bug. as pseudo-random
numbers - they should behave within a certain pattern - the only thing is
that srand which should seed the random number with specified number should
start from the same point but as it seems it seeds it with random number.
Can you test the little script I posted and post the result? Here on
5.2.6 CLI it works as expected.

Micha
Sep 12 '08 #8

P: n/a
A
Can you test the little script I posted and post the result? Here on
5.2.6 CLI it works as expected.
I did, first 3 numbers are random and second seq which should be identical
to the first one is again random (and on each reload of the page the first
and second seq are both random again). it's like srand would be commented
out of the code.

I notified server administrators to run the same script on other servers
running php 5.2.6 and will post their test results here.
Sep 12 '08 #9

P: n/a
Michael Fesser wrote:
.oO(Jerry Stuckle)
>I don't know about 5.2.3, but it is working as designed in 5.2.6.

Random numbers are exactly that - random (actually, in programming they
can only be pseudo-random). Your problem is you are depending on the
ability to repeat a sequence. That's not a good assumption to make in
any language.

Many languages allow to seed the random number generator, simply because
there are cases where you have to repeat the exact same sequence. You
still get random values, maybe even based on a random seed, but you're
able to reproduce them if necessary. Nothing wrong with that.

If there's such a seed function, then this is expected and documented
behaviour. With PHP's current MT implementation the seed function is
totally useless and the generator not backwards-compatible. I consider
this a bug or at least a really stupid design decision. If they would at
least have made this new behaviour optional.

rand()/srand() still seem to work correctly here (5.2.6):

<?php
srand(42);
for ($i = 3; $i--;) var_dump(rand());
srand(42);
for ($i = 3; $i--;) var_dump(rand());
?>

int(71876166)
int(708592740)
int(1483128881)
int(71876166)
int(708592740)
int(1483128881)

Micha
Micha,

Many languages have a seed function for compatibility only. Believe it
or not, there used to be a day when you didn't have a way to seed a
random number automatically. I still remember having to enter a number
when starting a program because there was no real time clock available.

And a "repeatable sequence of random numbers" is an oxymoron. Yes, many
people have used a random number generator as a quick way to generate a
repeatable sequence. I consider THAT a bug.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Sep 12 '08 #10

P: n/a
On Sep 12, 4:39*am, Jerry Stuckle <jstuck...@attglobal.netwrote:
And a "repeatable sequence of random numbers" is an oxymoron. *Yes, many
people have used a random number generator as a quick way to generate a
repeatable sequence. *I consider THAT a bug.
That's only because people are using the terminology colloquially.
What they mean is a "repeatable sequence of numbers which are
statistically random." It's entirely expected behavior that when
using the same seed one will get that. Especially if that's the
behavior of previous versions of the system.

However, http://www.php.net/manual/en/function.mt-srand.php does
state:

"Since 5.2.1 the Mersenne Twister implementation in PHP now uses a new
seeding algorithm by Richard Wagner. Identical seeds no longer produce
the same sequence of values they did in previous versions. This
behavior is not expected to change again, but it is considered unsafe
to rely upon it nonetheless."

The last sentence is quite important.

Walter
Sep 12 '08 #11

P: n/a
A
"Since 5.2.1 the Mersenne Twister implementation in PHP now uses a new
seeding algorithm by Richard Wagner. Identical seeds no longer produce
the same sequence of values they did in previous versions. This
behavior is not expected to change again, but it is considered unsafe
to rely upon it nonetheless."

I found somewhere on some forum that this is misstated. What this means is
that 5.2.0 and 5.2.1 will not produce the same sequence for the same seed,
however, the same seed will produce the same seq on specific php version in
other words, the sequences are not likely to change on future versions on
php e.g. 5.2.3, 5.2.6, 6.0 etc. but the seed will still function as it used
to.

anyway - the issue was somehow server related and not php related - the
administrator now fixed this script:

srand(42);
for ($i = 3; $i--;) echo rand()."<br />";
but my old one:

srand(1);
echo rand(0, 255)."<br />";
echo rand(0, 255)."<br />";
echo rand(0, 255)."<br />";

still produces random numbered sequences like srand is commented out. i'll
see if he can check that one out as well. i'll also ask him what was the
cause of this misbehaviour - maybe it is something to do with apache?
Sep 12 '08 #12

P: n/a
A
ok, here is the deal what happened here:

it appears that the latest build of suhosin module has an option to ignore
srand and mt_srand seeds and seed automatically. and the server php was
built with that option. where i used prebuilt module which obviously has
this option differently configured.

anyway, if anyone has an issue with that, now you know.
Sep 12 '08 #13

P: n/a
WalterGR wrote:
On Sep 12, 4:39 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
>And a "repeatable sequence of random numbers" is an oxymoron. Yes, many
people have used a random number generator as a quick way to generate a
repeatable sequence. I consider THAT a bug.

That's only because people are using the terminology colloquially.
What they mean is a "repeatable sequence of numbers which are
statistically random." It's entirely expected behavior that when
using the same seed one will get that. Especially if that's the
behavior of previous versions of the system.

However, http://www.php.net/manual/en/function.mt-srand.php does
state:

"Since 5.2.1 the Mersenne Twister implementation in PHP now uses a new
seeding algorithm by Richard Wagner. Identical seeds no longer produce
the same sequence of values they did in previous versions. This
behavior is not expected to change again, but it is considered unsafe
to rely upon it nonetheless."

The last sentence is quite important.

Walter
They use it only because they've become accustomed to it being a
repeatable sequence. Such an assumption is incorrect.

PHP's implementation is now much closer to a true random number generator.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Sep 12 '08 #14

P: n/a
>PHP's implementation is now much closer to a true random number generator.

PHP's implementation is much closer to a live goat than it is to a
true random number generator. So is any other pseudo-random number
generator.

It's a *PSEUDO*-random number generator, and if you use cryptography,
the difference between a true random number generator and a
pseudo-random number generator can get you executed as a spy.
Sep 12 '08 #15

P: n/a
Gordon Burditt wrote:
>PHP's implementation is now much closer to a true random number generator.

PHP's implementation is much closer to a live goat than it is to a
true random number generator. So is any other pseudo-random number
generator.

It's a *PSEUDO*-random number generator, and if you use cryptography,
the difference between a true random number generator and a
pseudo-random number generator can get you executed as a spy.
When it comes to random number generators, NO language provides a real
one. They are all pseudo-random.

So you're saying anyone who uses cryptography on a computer should be
executed as a spy.

The world would be a very lonely place...

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Sep 12 '08 #16

P: n/a
On Sep 12, 4:39 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
>
>And a "repeatable sequence of random numbers" is an oxymoron. Yes,
many people have used a random number generator as a quick way to
generate a repeatable sequence. I consider THAT a bug.

That's only because people are using the terminology colloquially.
What they mean is a "repeatable sequence of numbers which are
statistically random." It's entirely expected behavior that when
using the same seed one will get that. Especially if that's the
behavior of previous versions of the system.
umm, no, not unless that was the design goal, which I don't think it was
or it wouldn't have been "fixed". Random is random is ... . A
randomizer that regenerates the same numbers is not creating truly
random numbers, is it?
>
However, http://www.php.net/manual/en/function.mt-srand.php does
state:

"Since 5.2.1 the Mersenne Twister implementation in PHP now uses a new
seeding algorithm by Richard Wagner. Identical seeds no longer produce
the same sequence of values they did in previous versions. This
behavior is not expected to change again, but it is considered unsafe
to rely upon it nonetheless."

The last sentence is quite important.
Sounds more like a weasel-out statement to me, admitting that the "fix'
might be a little iffy.
>
Walter


Sep 12 '08 #17

P: n/a
..oO(Jerry Stuckle)
>Gordon Burditt wrote:
>>PHP's implementation is now much closer to a true random number generator.

PHP's implementation is much closer to a live goat than it is to a
true random number generator. So is any other pseudo-random number
generator.

It's a *PSEUDO*-random number generator, and if you use cryptography,
the difference between a true random number generator and a
pseudo-random number generator can get you executed as a spy.

When it comes to random number generators, NO language provides a real
one. They are all pseudo-random.

So you're saying anyone who uses cryptography on a computer should be
executed as a spy.

The world would be a very lonely place...
No more problems finding a parking space. ;)

Micha
Sep 12 '08 #18

P: n/a
..oO(Twayne)
>On Sep 12, 4:39 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
>>And a "repeatable sequence of random numbers" is an oxymoron. Yes,
many people have used a random number generator as a quick way to
generate a repeatable sequence. I consider THAT a bug.

That's only because people are using the terminology colloquially.
What they mean is a "repeatable sequence of numbers which are
statistically random." It's entirely expected behavior that when
using the same seed one will get that. Especially if that's the
behavior of previous versions of the system.

umm, no, not unless that was the design goal, which I don't think it was
or it wouldn't have been "fixed".
If it would have been a design mistake, then there wouldn't be a seed
function. But it exists, hence the previous behaviour was by intention.
>Random is random is ... . A
randomizer that regenerates the same numbers is not creating truly
random numbers, is it?
Truly random numbers require some more effort and can't be created with
software alone.

A software-based generator only creates pseudo-random numbers, based on
algorithms. Such algorithms are usually deterministic - same input, same
output.

Micha
Sep 12 '08 #19

P: n/a
>When it comes to random number generators, NO language provides a real
>one. They are all pseudo-random.
That's not an excuse to call any of them a true random number generator.

There are some hardware devices intended to generate random numbers
based on quantum theory (some built into Intel CPUs, for example).
Some operating systems give access to this, some through /dev/random.
Are there no languages that use the OS /dev/random?
>So you're saying anyone who uses cryptography on a computer should be
executed as a spy.
No, I'm saying that if they use crappy cryptography, they are more
likely to be CAUGHT and CONVICTED as a spy because their cryptography
is crappy.

Sep 12 '08 #20

P: n/a
.oO(Twayne)
>
>>On Sep 12, 4:39 am, Jerry Stuckle <jstuck...@attglobal.netwrote:

And a "repeatable sequence of random numbers" is an oxymoron. Yes,
many people have used a random number generator as a quick way to
generate a repeatable sequence. I consider THAT a bug.

That's only because people are using the terminology colloquially.
What they mean is a "repeatable sequence of numbers which are
statistically random." It's entirely expected behavior that when
using the same seed one will get that. Especially if that's the
behavior of previous versions of the system.

umm, no, not unless that was the design goal, which I don't think it
was or it wouldn't have been "fixed".

If it would have been a design mistake, then there wouldn't be a seed
function. But it exists, hence the previous behaviour was by
intention.
>Random is random is ... . A
randomizer that regenerates the same numbers is not creating truly
random numbers, is it?

Truly random numbers require some more effort and can't be created
with software alone.

A software-based generator only creates pseudo-random numbers, based
on algorithms. Such algorithms are usually deterministic - same
input, same output.
No arguement there. It's just that if I want "random" numbers I don't
want to see a number repeat itself more than once in great while
coincidence type manner. When I want a set of random numbers back, I
want to pull them from storage somewhere, not repeat the random number
generation to get them; that makes the name oxymoronic IMO; thus, a
misnomer of sorts.
I know, it's been discussed for eons so I won't work to draw this out
into long diatribes; it's kind of OT anyway at this point I think.
I remember my first experiences with random numbers back in the days
of CP/M basic and at first I thought it was pretty neat stuff until I
realized all it did was read a system timer and then manipulate it
accordingly<g>.
Now if I could just get my ISP to move up to something more current
than PHP 5.2.2 I'd really be all set.

Regards,

Twayne

>
Micha


Sep 13 '08 #21

This discussion thread is closed

Replies have been disabled for this discussion.