Click to Play

Syndicating Your Blog
WebProNews takes a look into how you can make money from blogging. It seems as though, syndication is an easy way to do so. We talked with President of Newstex...

Recent Articles

Playing With Perl 5.10
While we were all busy getting ready for the holidays, version 5.10 of Perl appeared on CPAN. That surprised me, because I thought the next Perl I'd see would be the dreaded Perl 6.. I don't know when that will be...

Equal Height CSS Columns With Filler Text
If you search for "column alignment" or "css columns", you'll probably eventually end up at In search of the One True Layout at positioniseverything.net. That's great stuff, but it is not what I wanted. I want to fill the left...

Random Subroutines In Perl
I'll bet your first question might be "why on earth would I ever want to call subroutines randomly?". Admittedly, it isn't a need that comes up that often, but (for example) it's used right here on this very page that you are...

Bouncing Around Perl On The Web
Once you have trounced the rest of the Perl class in Prisoner's Dilemma and are ready to take your skills into the field, it's good to know plenty of resources exist online. The drumbeat of PHP, Java, and Rails has...

Stripping Out Bad Characters Generated By A Perl...
There is an interesting thread in the WebProWorld forum that solves a problem a member was having trying to strip bad characters from a block of text generated by his perl shopping cart. The member's initial post reads...

ColdFusion Brands With Adobe, Shares Data with Perl
ColdFusion's latest version emerges for the first time under the Adobe brand name; it's changed a lot since its early dot-com days of doing database calls for HTML. The arrival of the public beta of Adobe ColdFusion 8...


02.08.08

Playing With Perl 5.10

By A.P. Lawrence

There is a simple mistake I make frequently with random numbers. I'll use Perl to illustrate, but trust me: I can screw this up in just about any language. And I have...

To make it both easy and obvious, let's say we want to randomly choose one of three things. Again to make it easy, we'll say that the integers 0, 1 and 2 will work for whatever it is we have in mind. So here's some code:



The "x" method is strongly biased toward the value of "1". That's because rand() returns values between 0 and 2 (but never 2), and int() always "rounds down" - it returns "1" for any number greater than 1 and less than 2. That biases the output toward 1. If that doesn't make sense to you, pretend for a moment that rand() could only generate integers or the .5 value between two integers - in other words, it could produce 0, 0.5, 1.0, and 1.5 only for rand(2) and 0, 0.5, 1.0, 1.5, 2.0 and 2.5 for rand(3). Here's what happens:


Do you see it now? Because int() doesn't round up as many of us seem to think it might, the x value clusters around "1"

Try a Better Way Today. Try WebEx PCNow

I've carelessly and unwittingly made that mistake many times. I know better, but I find myself doing this quite often. No doubt it comes from a desire to "round up", even though that's completely unnecessary for the task at hand. The most likely explanation is that seeing "int()" triggers memories of rounding and overrides my sense of what I'm actually trying to accomplish. I'm a bit on "automatic pilot" there, letting my subconscious fill in the details, and it wants to add .5 when it sees "int()".

When you are trying to choose three or more items, this mistake does damage, but limits it to the lower and upper elements. For example, the output when we use :



Only 0 and 5 get shorted. Not great, but at least we do get some "randomness" (biased as it is). However, when limited to just two elements, we may not even notice the mistake at all because it's unbiased. To see that, this time pretend that rand() can only pick in increments of .25:


So if just picking betweeen two choices, the mistake is harmless.. but because it is harmful for larger sets, you really shouldn't get into the habit.

Comments

*Originally published at APLawrence.com

About the Author:
A.P. Lawrence provides SCO Unix and Linux consulting services http://www.pcunix.com


About PerlProNews
PerlProNews is a collection of news and commentary designed to keep you in step with the ever evolving landscape of Perl environments. News and Advice for Perl Professionals

PerlProNews is brought to you by:

SecurityConfig.com NetworkingFiles.com
NetworkNewz.com WebProASP.com
PerlProNews.com SQLProNews.com
SysAdminNews DevWebPro.com
LinuxProNews.com WirelessProNews.com
CProgrammingTrends.com ITCertificationNews.com


-- PerlProNews
is an iEntry, Inc. publication --
iEntry, 2549 Richmond Rd. Lexington KY, 40509
2008 iEntry, Inc.  All Rights Reserved  Privacy Policy  Legal

archives | advertising info | news headlines | free newsletters | comments/feedback | submit article



Database Forum News and Advice for Perl Professionals PerlProNews News Archives About Us Feedback PerlProNews.com About Article Archive News Downloads WebProWorld Forums iEntry Advertise Contact Jayde