View Full Version : Coin Tossing CGI Program Help

07-01-2005, 11:14 AM
Hey, I'm new to cgi and perl and I've been trying to put together a cgi that will act as a virtual coin tossing program, but I just keep getting error messages popping up all over the place.

In other words . . . I'm wanting to put together a cgi that will pick a random number between 1 or 2 (heads or tails), and then add that choice to a poll. Each time someone activates the cgi, it picks a random number and adds that to the poll to show how many heads have been flipped and how many tails have been flipped. It seemed like a really easy thing to do, but I spent all day trying to put it together without any success. :confused:

Any help would really be greatly appreciated. Thanks.


07-01-2005, 12:51 PM
If this is a homework project, we can offer hints.

It's best if we don't just show you a script ... you'll learn better if you figure it out yourself.

What part fails? The saving of data? The random number part?

Use Google to find some examples (include the word "perl"):

search for: perl random number example
search for: perl write file example

07-01-2005, 05:10 PM
Ok, I've found out how to make a basic random number generator for heads or tails based on the second that the page is opened. The cgi script looks something like this . . .

print "Content-type: text/html\n\n";
my($sec ,$min,$hr,$mday,$month,$year,$dayofweek,$dayofyear,$isdst) = localtime(time);
my $flip;
if ($sec == 0) {
$flip = "heads";
if ($sec == 1) {
$flip = "tails";

. . . . and the if's go on until 59. I'm sure there's a much simpler way of making all even numbers = heads and all odd numbers = tails, but I don't know one. . . .

print "Second is $sec.<br>\n";
print "The coin landed on $flip.\n";

Now I'm completely in the dark about how to actually put this into a poll and record all the heads and tails. Hints? Help? I'd really appreciate it. Thanks


07-01-2005, 05:52 PM
after a simple google search for random perl numbers I got to this page:


Just..to simplify your code. But honestly, google is your best friend especially with a problem such as this...

07-01-2005, 06:45 PM
Jacob ...

Your method might not produce the results you need, but I want you to read some tutorials and figure it out. I don't want to just give you the random number example.

You then need to store the results in a simple text file. Find that example
using Google. Once you store the results ...

You now have some decisions to make about how to impliment it on your webpage.

1) You can use SSI (if your webhost allows it) - Server Side Includes.
That will display the new count of heads and tail each time the page is
refreshed or visited by the visitor. They would push a button to flip the
coin each time ... the page would re-display to show new results.

2) You can use Javascripting and Cookies to execute the script automatically and display the results each time the page is refreshed or visited.
... or, they would have to push a button, or both.
They would need to have Javascripting and Cookies enabled for this to work.

3) You can display a separate page with the results and not have it displayed
on your web site.

#1 and #3 are easiest. #2 requires some fancy tricks

Your decision will determine the next step.