473,394 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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
20 5062
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
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
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
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
..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
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
..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
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
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
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
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
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
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
>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
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
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
..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
..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
>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
.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Joe | last post by:
Hi, I have been working on some code that requires a high use of random numbers within. Mostly I either have to either: 1) flip a coin i.e. 0 or 1, or 2) generate a double between 0 and 1. I...
4
by: Jack | last post by:
I have two files: sort_comparison.c++ my_sort.h sort_comparison.c++ calls this code in my_sort.h: void my_sort::fillArray(int arr,int n) { // const int random_number_range=1000000;
70
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like...
11
by: Mark Rae | last post by:
Hi, My R&D department has asked me to look at threading in a Web Service written in C#, so I came up with the following code: using System; using System.ComponentModel; using...
6
by: Intiha | last post by:
Hello all, I am trying to generate random seeds for my simulations. currently i was using srand(time(NULL); for this purpose. But for confidence in my results i ran it using a script in a loop....
8
by: Anil Gupte | last post by:
I had someone write a random number generator in C# (I am more of a VB programmer) and they came up with the following: public string GetRand(int count) { string number = ""; for (int i=0;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.