PHP rand(0,1) on Windows < OpenSSL rand() on Debian

512x512 image made using `if rand(0,1)` on PHP/Windows
(Please excuse the inflammatory title.)
Bo Allen recently made a post regarding the difference between true random and pseudorandom numbers, using 512x512 bitmaps as illustration.
Some might doubt his result on Windows, as the image seems to be too predictable. Initially, I passed it off as PHP using the default Windows pseudorandom number generator, which is a Linear Congruential Generator.
The default windows rand() function is implemented as follows (coefficients from this site, many others have them also):
unsigned long randState = 0;
//Get random number
unsigned long rand(void) {
randState = randState * 214013 + 2531011L;
return (randState >> 16) & 0x7fff;
}
//Seed RNG
void srand(unsigned long seed) {
randState = seed;
}
Here is Bo Allen's code:
// Requires the GD Library
header ("Content-type: image/png");
$im = @imagecreatetruecolor(512, 512)
or die("Cannot Initialize new GD image stream");
$white = imagecolorallocate($im, 255, 255, 255);
for($y=0;$y<512;$y++){
for($x=0;$x<512;$x++){
if(rand(0,1)===1){
imagesetpixel($im, $x, $y, $white);
}
}
$x=0;
}
imagepng($im);
imagedestroy($im);
Let's have a look at how PHP does rand(0,1). All code is from the php-5.2.6 source release.
//ext/standard/rand.c:296-315
/* {{{ proto int rand([int min, int max])
Returns a random number */
PHP_FUNCTION(rand)
{
long min;
long max;
long number;
int argc = ZEND_NUM_ARGS();
if (argc != 0 &&
zend_parse_parameters(argc TSRMLS_CC, "ll", &min, &max) == FAILURE)
return;
number = php_rand(TSRMLS_C);
if (argc == 2) {
RAND_RANGE(number, min, max, PHP_RAND_MAX);
}
RETURN_LONG(number);
}
/* }}} */
A bit of digging reveals that php_rand is just a wrapper to the system's random() function. Okay, so what exactly is RAND_RANGE?
//ext/standard/php_rand.h:43-44
#define RAND_RANGE(__n, __min, __max, __tmax) \
(__n) = (__min) + (long) ((double) ( (double) (__max) - (__min) + 1.0) \
* ((__n) / ((__tmax) + 1.0)))
Wow, that is some ugly code.
Python's default random generator uses Mersenne Twister, which is an excellent choice for any application outside of cryptography. Here's code that replicates Bo's experiment, including a re-implementation of php's broken rand() function.
import PIL.Image, random, time
class MsftRand(random.Random):
'''This is equivalent to rand() and srand() on the Windows platform'''
state = 0
def __init__(self): # constructor method
self.state = int(time.time()) # seconds since the epoch
def seed(self,s):
self.state = s
def setstate(self,s):
self.state = s
def getstate(self):
return self.state
def random(self): # returns a float [0,1)
self.state = (self.state * 214013 + 2531011) % 2**32
return float((self.state >> 16) & 0x7fff) / 2**15
class bogoRand(MsftRand):
'''This replicates how PHP does randint()'''
def randint(self, a, b):
self.state = (self.state * 214013 + 2531011) % 2**32
return a + int(float((float(b-a+1)*((self.state&0x7fff)/((1<<15)+1.0)))))
image = PIL.Image.new("1",(512,512)) # Creates a 512x512 bitmap
# creates a list of different RNG objects and names
rand_mapping = [ (MsftRand(), "Microsoft"),
(random.Random(), "Mersenne"),
(bogoRand(), "bogoRand") ]
for rnd, name in rand_mapping:
for y in xrange(512):
for x in xrange(512):
image.putpixel((x,y), rnd.randint(0,1))
image.save("random_"+name+".png")
This will create three images when you run it--random_Mersenne.png, which uses Python's default random generator and randint() implementations, random_Microsoft.png, which uses the Windows LCG and Python's randint() implementation, and random_bogoRand.png, which uses the Windows LCG and PHP's randint() implementation.
Here's what random_bogoRand.png looks like:

The other two images have no patterns obvious to the eye. How does Python do randint()?
from Lib/rand.py:
def randint(self, a, b):
"""Return random integer in range [a, b], including both end points.
"""
return self.randrange(a, b+1)
def randrange(self, start, stop):
#I'm paraphrasing here, it does checks to make sure the arguments
# are integers and other sanity stuff
return start + int(self.random() * (stop - start))
It's not that PRNGs are visibly flawed, it's that PHP has a terrible method of generating a random range.
Labels: PHP, Programming, Python, Random
80 Comments:
Wow, excellent follow-up! I appreciate the time you spent looking into the details of this.
Using PHP's mt_rand() function (which utilizes the Mersenne Twister) provides a better random value.
After digging deeper into this, it seems likely that the real problem is two-fold:
first, rand() on Win32 has a period of 32767;
secondly, the RAND_RANGE define is trying to scale the generated number into the range you requested. This can result in some loss of randomness (see http://bugs.php.net/bug.php?id=45184).
Python's randrange() solves this by calling _randbelow() in a loop while you're outside the requested range - which is probably the best way to do it. There is some work on this that should be coming out as of 5.3 - thanks for bringing it up!
deja-vu on random.org
Even with PHP 5.3, the results aren't that much different. Using mt_rand instead, though, is mind-blowing...
PHP 5.3 with rand: http://twitpic.com/gq81b
PHP 5.3 with mt_rand: http://twitpic.com/gq827
Yes, you have to use mt_rand. That's why they describe it as "Generate a better random value".
If you want to do a comparison at least make it a meaningful one and use the rand function the way it is intended:
This will give your bit: "rand() & 1;", so imagesetpixel($im,$x,$y,rand() &1);
will do the job. To save you the typing you can look here:
http://ww.com/random.php
That's on a linux box running php 4.4.2, the results look pretty random to me. To make the result reproducible I've seeded the PRNG with '0' using srand(0); at the beginning of the program.
If you want to use the 'scale' function in PHP for something that you should be doing by a one bit and then you will reap the results of that, you are effectively sampling the lowest bit after a bunch of float operations, which is clearly sub-optimal.
PHP can't correct for incorrect use.
As for the rest of your code, the $x=0; line can be dropped, there is no need to 'reset' your loop variable at the end of a loop, last I checked.
I'm no PHP fan but before you criticize a project make sure you understand the material.
http://pastebin.com/m40b85179
Public domain. Have fun.
@jacquesm
1.) Linux was never the problem. Read the post title...
2.) Your code is wrong.
imagesetpixel($im,$x,$y,rand() &1);
...Pretty much just makes a black square.
3.) After fixing your code, your bitwise use of rand() on Windows actually makes an even worse random graphic.
4.) rand(0, 1) works just fine. Yes, rand() &1 is faster, but that doesn't mean rand(0, 1) is "wrong"... Especially since, in this case, rand(0, 1) produces better randomness.
Your analysis is completely wrong.
You made a mistake in the code for bogoRand. IF php were to call windows random(), the call would be:
return a + int(float((float(b-a+1)*((self.state&0x7fff >> 16)/((1<<15)+1.0)))))
You forgot the ">> 16", I hope it's not intentional. But IF php were to call windows random(), there would be no problem, as anyone can see by adding back the ">> 16" that is missing.
What's really happening is that PHP in the ZTS mode (I don't know what it stands for, but it has something to do with Thread Safety) uses its own version of random() which is defined by:
return ((*ctx = *ctx * 1103515245 + 12345) % ((u_long)PHP_RAND_MAX + 1));
And you can test that by replacing your code for bogoRand by:
def randint(self, a, b):
self.state = (self.state* 1103515245 + 12345) % (1 << 15)
return a + int(float((float(b-a+1)*((self.state/((1<<15)+1.0))))))
you will obtain EXACTLY what happens in Bo Allen post.
Thank you !! Very usefull !!
vb dot net training in chennai
csharp training in chennai
THanks for the insightful article goDigitally
Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.
hadoop-training-institute-in-chennai
Nice Info!
I known the difference between random numbers from your blog.you also refer the logic for php for cse project centers in chennai.
Thanks.
Thank you for posting this useful information
Big Data Training Chennai | Best Hadoop Training in Chennai
Nice blog! Thank you for sharing valuable information.
Hadoop Training in Chennai | Android Training in Chennai
Thank you so much for sharing this worth able content with us. The concept taken here will be useful for my future programs and i will surely implement them in my study. Keep blogging article like this.
web designing course in chennai
Thanks a lot for your information.
PHP Training in Chennai
Wonderful post!!Thank you for sharing this info with us.
Keep updating I would like to know more updates on this topic
Very useful content, I would like to suggest this blog to my friends.
PHP Course in Chennai
PHP Training Center in Chennai
wow great Article, the details you have provided are much clear, easy to understand, if you post some more Article, it will be very much useful for me.
PHP Training in Chennai
It’s great to come across a blog every once in a while that isn’t the same out of date rehashed material. Fantastic read.
Digital Marketing Training in Mumbai
Six Sigma Training in Dubai
Six Sigma Abu Dhabi
Thanks for sharing the information, keep updating us.This information is really useful to me
dot net training in chennai
Useful post.Thank you for sharing such an important data.Keep updating for this blog.
PHP Training in Chennai
PHP training in velachery
PHP training in Velachery
Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
Good discussion. Thank you.
Anexas
Six Sigma Training in Abu Dhabi
Six Sigma Training in Dammam
Six Sigma Training in Riyadh
Awesome Post. Thanks for Sharing. Kepp updating.
Html5 Training in Chennai
Html5 Courses in Chennai
Html5 Training
Html5 Course
Html5 Training Course
Html5 Training for Beginners
Best Html5 Course
Best Html5 Training
Thank you for sharing this post.
Education
Technology
Awwsome informative blog ,Very good information thanks for sharing such wonderful blog with us ,after long time came across such knowlegeble blog. keep sharing such informative blog with us.
Aviation Academy in Chennai | Aviation Courses in Chennai | Best Aviation Academy in Chennai | Aviation Institute in Chennai | Aviation Training in Chennai
Thanks for the valuable information and insights you have so provided here... SEO Training
This comment has been removed by the author.
Nice post By reading your blog, i get inspired and this provides some useful information. Thank you for posting this exclusive post for our vision.
Digital Marketing Training
Nice Blog thanks for the blog. Good to share with my friends.
Wedding Photographer
Thanks for such a great article here. I was searching for something like this for quite a long time and at last I’ve found it on your blog. It was definitely interesting for me to read about their market situation nowadays.Well written article on codefiles. Thank You Sharing with Us.android code structure best practices |
android development guide
| demand for android developers 2018
Hi, thanks for sharing such an informative blog. I have read your blog and I gathered some needful information from your blog. Keep update your blog. Awaiting for your next update.
ibm datapower online training
Thanks for sharing this information admin, it helps me to learn new things. Continue sharing more like this.
ReactJS Training in Chennai
ReactJS course in Chennai
RPA courses in Chennai
AWS Certification in Chennai
Angular 5 Training in Chennai
Angularjs Training in Chennai
Your post is really awesome. Your blog is really helpful for me to develop my skills in a right way. Thanks for sharing this unique information with us.
- Learn Digital Academy
Good job in presenting the correct content with the clear explanation. The content looks real with valid information. Good Work
DevOps is currently a popular model currently organizations all over the world moving towards to it. Your post gave a clear idea about knowing the DevOps model and its importance.
Good to learn about DevOps at this time.
devops training in chennai | devops training in chennai with placement | devops training in chennai omr | devops training in velachery | devops training in chennai tambaram | devops institutes in chennai | devops certification in chennai | trending technologies list 2018
ieee projects in chennai
More informative,thanks for sharing with us.
this blog makes the readers more enjoyable.keep add more info on your page.
Best Cloud Computing Training Institute in Anna nagar
Cloud Computing Training in Ambattur
Cloud Computing Certification Training in T nagar
Cloud Computing Courses in T nagar
Cloud Computing Training Institutes in OMR
Cloud Computing Courses in OMR
Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information.
best big data training in chennai
Big Data Hadoop Training
Hadoop training institutes in chennai
CCNA certification in Chennai
CCNA Training
CCNA courses in Chennai
very useful post thanks for sharing
vyaparpages
Education
Great share !!
ieee final year projects in chennai
Java projects in chennai
Software projects in chennai
Very informative and well written post! Quite interesting and nice topic chosen for the post.
thanks for sharing this nice post,
excel training in hyderabad
Great share !!
ieee final year projects in chennai
Java projects in chennai
Software projects in chennai
Great Posting…
Keep doing it…
Thanks
Digital Marketing Certification Course in Chennai - Eminent Digital Academy
Great share !!
ieee final year projects in chennai
Robotics projects in chennai
Arduino projects in chennai
Informative post, thanks for sharing.
Article submission sites
Guest posting sites
Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
industrial safety course in chennai
Appreciation for really being thoughtful and also for deciding on certain marvelous guides most people really want to be aware of.
occupational health and safety course in chennai
builderall is very easy to use and cheap too.
Amazing Post. The choice of words is very unique. Interesting idea. Looking forward for your next post.
Hadoop Admin Training in Chennai
Hadoop Administration Training in Chennai
Hadoop Administration Course in Chennai
Hadoop Administration Training
Big Data Administrator Training
IELTS coaching in Chennai
IELTS Training in Chennai
SAS Training in Chennai
SAS Course in Chennai
Very good post.
All the ways that you suggested for find a new post was very good.
Keep doing posting and thanks for sharing.
mainframe training in hyderabad
mobile repairing course in hyderabad
Magnificent blog!!! Thanks for your sharing… waiting for your new updates.
PHP Course in Madurai
PHP Training Center in Chennai
PHP Training Center in Coimbatore
PHP Training Institute in Coimbatore
PHP Training in Chennai
Nice Post..Thanks for Sharing..
erp in chennai
erp providers in chennai
SAP B1 support chennai
SAP S/4 Hana support chennai
SAP R3 support chennai
hr outsourcing
Hello there! This is my first comment here, so I just wanted to give a quick shout out and say I genuinely enjoy reading your articles.
nebosh course in chennai
This comment has been removed by the author.
Amazing Post. Your writing is very inspiring. Thanks for Posting.
Ethical Hacking Course in Chennai
Hacking Course in Chennai
Ethical Hacking Training in Chennai
Certified Ethical Hacking Course in Chennai
Ethical Hacking Course
Ethical Hacking Certification
Node JS Training in Chennai
Node JS Course in Chennai
Thanks for spending time and your effort to create such an article like this is a wonderful thing. I’ll learn many new info!
German Classes in Chennai
German Language Classes in Chennai
Big Data Training in Chennai
Hadoop Training in Chennai
Android Training in Chennai
Selenium Training in Chennai
Digital Marketing Training in Chennai
JAVA Training in Chennai
Big Data Training
information
information
Thanks for sharing this wonderful article. Your article is very interesting to read. Looking forward to read ur future post.
Node JS Training in Chennai
Node JS Course in Chennai
Node JS Advanced Training
Node JS Training Institute in chennai
Node JS Course
Node JS Training in Velachery
Node JS Training in Tambaram
Node JS Training in Adyar
Thank you for sharing this wonderful post with us. You are providing very valuable information. Looking forward to learn more from you.
Mobile Testing Training in Chennai
Mobile Testing Course in Chennai
Oracle Training in Chennai
Manual Testing Training in Chennai
Corporate Training in Chennai
WordPress Training in Chennai
Great casino, ever casinos online you can safely put the top ten, with the new year already playing. At first I didn’t enter the topic as it is not hung with all sorts of bells and whistles I used to, but over time you realize that only good gaming games, bonuses and timely payments are needed in the casino, everything else interferes and distracts.
The blog is very much informative... Thanks for your updates...
data analytics courses in bangalore
data analysis courses in bangalore
RPA training in bangalore
Selenium Training in Bangalore
Java Training in Madurai
Oracle Training in Coimbatore
PHP Training in Coimbatore
Your writing skill is too good and article shared is really fantastic
Big Data Training in Chennai
Data Science Training in Chennai
Devops Training in Chennai
AWS Training in Chennai
RPA Training in Chennai
Hadoop Training in Chennai
Great share !!!
Mobile app development
UX UI training in chennai
Adobe Photoshop training in chennai
The content you have given is Extra-Ordinary. Very interesting write-up. Looking forward for your next Post.
Ethical Hacking Course in Chennai
Hacking Course in Chennai
Certified Ethical Hacking Course in Chennai
Ethical Hacking Course in Velachery
SAS Training in Chennai
SAS Course in Chennai
Informatica Training in Chennai
Informatica course in Chennai
This is the very good post... Thanks for sharing with us... It is more informative...
Web Designing Course in Coimbatore
Web Design Training in Coimbatore
Web Designing Course in Madurai
Java Course in Bangalore
Devops Training in Bangalore
Digital Marketing Courses in Bangalore
German Language Course in Madurai
Cloud Computing Courses in Coimbatore
Embedded Course in Coimbatore
Nice article I was really impressed by seeing this blog, it was very interesting and it is very useful for me.
CCNA Course in Chennai
CCNA Training in Chennai
RPA Training in Chennai
Ethical Hacking Course in Chennai
Blue Prism Training in Chennai
CCNA Classes in Tambaram
CCNA Training in OMR
CCNA Training in Velachery
Excellent post, it will be definitely helpful for many people. Keep posting more like this.
DevOps certification in Chennai
DevOps Training in Chennai
Data Science Course in Chennai
Data Science Training in Chennai
DevOps Training in Velachery
DevOps Training in Tambaram
There is a desire to earn a lot? Then come to us and win. top slot machine games online .Fate favors you, come in and win.
Very nice post here thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
machine learning course fees in chennai
machine learning training center in chennai
machine learning with python course in chennai
Wow cool site I have already withdrawn money from here. Actually my girlfriend advised me and I decided to risk the risk and you elegantplay casino slot games I wish you more victories
Click here |Norton Customer Service
Click here |Mcafee Customer Service
Click here |Phone number for Malwarebytes
Click here |Hp printer support number
Click here |Canon printer support online
Nice post. Thanks for sharing! I want people to know just how good this information is in your article. It’s interesting content and Great work.
Thanks & Regards,
VRIT Professionals,
No.1 Leading Web Designing Training Institute In Chennai.
And also those who are looking for
Web Designing Training Institute in Chennai
SEO Training Institute in Chennai
PHP & Mysql Training Institute in Chennai
Photoshop Training Institute in Chennai
Android Training Institute in Chennai
Happy to read your blog and gained a lot of new information in detail. Thanks for sharing.
IELTS Coaching in Chennai
IELTS Coaching Centre in Chennai
IELTS Training in Chennai
Best IELTS Coaching in Chennai
IELTS Classes in Mumbai
IELTS Coaching in Mumbai
Best IELTS Coaching in Mumbai
IELTS Center in Mumbai
The blog which you have shared is more informative. thanks for your sharing...
German Classes in Bangalore
German coaching classes in Coimbatore
German Classes in Coimbatore
Java Training in Bangalore
Python Training in Bangalore
IELTS Coaching in Madurai
IELTS Coaching in Coimbatore
Java Training in Coimbatore
Thank you for excellent article.
Please refer below if you are looking for best project center in coimbatore
soft skill training in coimbatore
final year projects in coimbatore
Spoken English Training in coimbatore
final year projects for CSE in coimbatore
final year projects for IT in coimbatore
final year projects for ECE in coimbatore
final year projects for EEE in coimbatore
final year projects for Mechanical in coimbatore
final year projects for Instrumentation in coimbatore
Awesome article! You are providing us very valid information. This is worth reading. Keep sharing more such articles.
Tally Course in Chennai
Tally Classes in Chennai
corporate training in chennai
Excel classes in Chennai
Tally Course in Anna Nagar
Tally Course in Velachery
Very nice blog, Thank you for providing good information.
Aviation Academy in Chennai
Air hostess training in Chennai
Airport management courses in Chennai
Ground staff training in Chennai
aviation training in chennai
cabin crew training institute in chennai
airlines training chennai
airport ground staff training in chennai
Nice Article !!!
luxury properties for sale in chennai
cheap luxury homes for sale in chennai
top luxury apartments in chennai
Nice post. Thanks for sharing! I want people to know just how good this information is in your article. It’s interesting content and Great work.
Thanks & Regards,
VRIT Professionals,
No.1 Leading Web Designing Training Institute In Chennai.
And also those who are looking for
Web Designing Training Institute in Chennai
SEO Training Institute in Chennai
Photoshop Training Institute in Chennai
PHP & Mysql Training Institute in Chennai
Android Training Institute in Chennai
Алюминиевый светодиодный профиль для led ленты я обычно беру в Ekodio там достойное качество и отличные цены.
Post a Comment
Subscribe to Post Comments [Atom]
<< Home