in reply to Creating a random generator

A simple approach is to make a list, then scale the internal random number generator to the length of the list, and use the number generated to pick an element of the list. This is a simple example:

#!/usr/bin/perl # use warnings; use strict; my @colors = qw(red yellow green cyan blue magenta white gray black); my $col = int(rand(scalar @colors)); print "Color = $colors[$col]\n";
Typical output is:

C:\Code>perl choose_color_1.pl Color = magenta C:\Code>perl choose_color_1.pl Color = red C:\Code>perl choose_color_1.pl Color = black

Replies are listed 'Best First'.
Re^2: Creating a random generator
by Lady_Aleena (Priest) on Sep 07, 2007 at 07:46 UTC

    esper, andreas1234567, and dwm042;

    There are tiny variations between the code that you gave me. Will that make a difference? I would like to have as little white space as possible. So are all the carriage returns needed between the lines?

    Now that I have the .pl file, how would I integrate this onto a web page? I do not have Perl installed on my computer at home so the only way to see it working is using on a web page.

    Update 1

    dwm042...On the following line, does "int" mean interger?

    my $col = int(rand(scalar @colors));

    To all

    Can the items in the array(?) be put on seperate lines? In the actual generator I would like to create each item will be a phrase, a sentence, or a table and most will consist of a lot of html code.

    Update 2

    What does the qw mean on the following line? Found it in a search.

    my @colors = qw(red yellow green cyan blue magenta white gray black);

    Update 3

    The above scripts are all causing 500 Internal Service Errors when I try to run them. I CHMOD 755'd them, was that wrong? Found the fix. I needed to add the following code.

    print "content-type: text/html \n\n";

      The first step will be to install Perl on your computer. If you have a machine running Microsoft Windows, download and install ActiveState Perl. If you have an Apple computer, chances are good that Perl already is installed. If you have any other brand of computer, the chance that it already has Perl installed isn't that bad either, but ask your machine administrator on how to access it.

      Then, you can test the .pl files directly by saving them from a text editor and then running them. On Windows, notepad.exe is an ugly but preinstalled editor. Word and Wordpad are unsuitable for editing programs. On the Apple products I don't know what built-in editors there are except vi and vi is an editor you want to avoid when starting out.

      So, if you have saved your Perl program as myprogram.pl, you can give it a local test run by opening a command prompt in the directory you saved the file to and typing

      perl -w myprogram.pl

      If there were any errors, Perl will tell you so. If not, look if the output matches what you expect.

      After that, you can upload the Perl file myprogram.pl to your web account, possibly into the cgi-bin directory, make it executable and then link to that.

        Corion;

        My only interest in Perl is the web application of it for this one project for my website. As the output will be a .shtml file, which I can not view locally anyway, having Perl on my computer would be redundant as my server already has it on theirs. My hard drive space is very limited as well. I have been making text/html files out of almost all of my MS Word files, since MS word files are huge in comparison. If I knew how to make a web page act like Excel, I would have almost all of my spreadsheets as web pages which would hopefully be smaller.