Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Coin Tossing CGI Program Help

    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.

    Any help would really be greatly appreciated. Thanks.

    Jacob

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,531
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    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

  • #3
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 . . .

    #!/usr/bin/perl
    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

    Jacob

  • #4
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    after a simple google search for random perl numbers I got to this page:

    http://www.unix.org.ua/orelly/perl/cookbook/ch02_08.htm

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

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,531
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    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.
    Last edited by mlseim; 07-01-2005 at 07:48 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •