Random Selection Fun
You're probably familiar with the concept of randomly
selecting one from a group.
Blindly drawing a slip of paper out of a box is an example.
If the box contained 4 slips of paper, marked "red,"
"green," "blue," and "yellow," drawing one slip would
be a random selection.
Computerized random selections are pseudo-random, sometimes
tending to slant toward certain results, depending on the
randomizing algorithm and the operating system the program
is running on. Computer randomization is good enough for
most requirements, selections tending to even out over many
"drawings."
However, a person might want to influence the results in
certain situations. Maybe a business person has 8000
specialty advertising pens to give away -- 1000 red pens,
1000 green pens, 4000 blue pens, and 2000 yellow pens. I'll
show you how to implement that influence.
Going back to the box with slips of paper drawing analogy,
8000 people would be lined up to participate in the drawing.
(We'll just say it's so; you don't have to imagine people
lined up for blocks.)
The drawing box would contain 1 red slip, 1 green slip,
4 blue slips, and 2 yellow slips, which is the ratio of
the colors of the pens. Whenever a person draws a slip, the
person is give a pen of that color. Then the slip is put
back into the box before the next person draws. That
procedure tends to give the pens away in a ratio comparable
to the available colors.
A computerized version is easy to implement with Perl:
#!/usr/bin/perl
use strict;
srand;
my %Selections = (
'red' => 1,
'green' => 1,
'blue' => 4,
'yellow' => 2,
);
my $Total = 0;
my %Individual = ();
for my $name (keys %Selections)
{
$Selections{$name} = 1 unless $Selections{$name} > 1;
for(1..$Selections{$name})
{
$Individual{$Total} = $name;
$Total++;
}
}
my $Answer = $Individual{int rand $Total};
print "Content-Type: text/html\n\n";
print <<HTML;
<head>
<body bgcolor="white">
<table border=""1"" cellpadding="12"><tr><td>
Congratulations! You have won a $Answer pen!
</td></tr></table>
</body>
</html>
HTML
The first 3 lines tell the server where Perl is located,
limit certain uses of functions and variables, and seed
the random number generator.
Lines 5 through 10 store the selections. Lines 6 through 9
are the actual lines of available selections. They list the
name of the available colors and the number of slips for
that color. A "=>" character pair separates the name and
number, and the line ends with a comma. The name is between
apostrophes, the number is not.
When you modify the lines of available selections for your
own use, you may have as many name and number selections as
you wish. If any of the names contain an apostrophe, the
apostrophe must be preceded with a backward slash.
Example: \'
Lines 12 through 23 determine the total number of choices
and "randomly" select one.
The browser printing section is composed of lines 25 through
34. The program above prints the result in its own window,
which could be a popup window. If you prefer to use SSI,
change the browser printing section to:
print "Content-Type: text/html\n\n";
print <<HTML;
Congratulations! You have won a $Answer pen!
HTML
For fun, the lines of available selections can be modified
to simulate the random answers magic cube or 8-ball
divination games provide. http://ofb.net/8ball/answers.html
has a list of possible answer selections. (The name of the
physical 8-ball game, which starts with the word "magic,"
is trademarked by a toy company.)
For business, the lines of available selections can be
modified to present sales or discounts. The lines of
available selections might be something like this:
'25% off - $75 instead of $100.00!' => 10,
'30% off - $70 instead of $100.00!' => 6,
'35% off - $65 instead of $100.00!' => 2,
'45% off - $55 instead of $100.00!' => 1,
'50% off - $50 instead of $100.00!' => 1,
The above would tend to provide a 25% discount 10 times out
of 20, and a 50% discount 1 time out of 20. The browser
printing section could be:
print "Content-Type: text/html\n\n";
print <<HTML;
$Answer
HTML
To display a pseudo-random photo, albeit slanted toward your
own preference, the lines of available selections might be:
'<img src=""a.jpg">'" => 1;
'<img src=""b.jpg">'" => 3;
'<img src=""c.jpg">'" => 1;
The program can be used as a "random" destination selector,
too. The lines of available selections might be:
'http://AffinityNumerology.com' => 1,
'http://BontragerConnection.com' => 2,
'http://WillMaster.com' => 3,
'/support' => 3,
In order to redirect the browser instead of simply printing
something, the browser printing section will need to be
changed to:
print "Location: $Answer\n\n";
And, the link on your web page might be:
<a href=""/cgi-bin/RandomMagic.cgi">"
Click for random destination!
</a>
If you plan on creating an on-line casino, don't tell the
patrons how easy it is to influence results :)
Question:
Did you find this article interesting and understandable? How can it be improved?
Your response is anonymous.
When done typing, click anywhere outside the box. [more info]
Will Bontrager
©2001 Bontrager Connection, LLC
Please note:
Articles on this website are presented "as is". However -
If you have a question about a CGI script, HTML, CSS, PHP, or JavaScript
Ask one of our Experts and you'll have your answer!
Click here for details.