473,586 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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? pleeeeeeaseeeee eee! :)
Sep 12 '08 #1
20 5096
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? pleeeeeeaseeeee eee! :)
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*******@attgl obal.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*******@attgl obal.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*******@attgl obal.net
=============== ===

Sep 12 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
7370
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 have utilised the following random number source code http://www.agner.org/random/ What I have found is that there is a problem with seeding. The...
4
2692
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
6222
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 using the high-order bits returned by rand() instead of the low-order bits, avoiding using rand() for anything that wants decently random numbers, not...
11
1819
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 System.Threading; using System.Web.Services;
6
5899
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. Since the time b/w execution is very similar, many simulation runs resulted in exact same results. Is there a better way of seeding the random...
8
7553
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; i<count; i++) { Random Rnd = new Random(); number = number+Convert.ToString(Rnd.Next(0,9));
0
7912
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8202
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8338
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.