A random storytelling program written by Perl (Rand random function) _ Application Tips

Source: Internet
Author: User
Tags rand scalar
code show as below:

#! / bin / perl
use strict;
use warnings;

#Defining variables
my $ count;
my $ input;
my $ number;
my $ sentence;
my $ story;

#Define four arrays
#Defined character array
my @ nouns =
(
   'Dad',
   'TV',
   'Mom',
   'Groucho',
   'Rebecca',
   'Harpo',
   'Robin Hood',
   'Joe and Moe',
);

#Define the action array
my @ verbs =
(
   'ran to',
   'giggled with',
   'put hot sauce into the orange juice of',
   'exploded',
   'dissolved',
   'dissolved',
   'sang stupid songs with',
   'jumped with',
 );

 #Defines array of locations
 my @ prepositions =
 (
     'at the store',
     'over the rainbow',
     'at the beach',
     'before dinner',
     'in New York City',
     'in a dream',
     'around the world',
 );

 do
 {
    #Every time before running, clear the contents of $ story
    $ story = '';
    #The following is a random combination to generate a story
    for ($ count = 0; $ count <6; $ count ++)
    {
       # Let's divide the structure, scalar @nouns is to get the number of @nouns array elements, and then use rand to generate a few randomly, and finally int is to round the obtained number
       $ sentence = $ nouns [int (rand (scalar @nouns))]
       . ""
       . $ verbs [int (rand (scalar @verbs))]
       . ""
       . $ nouns [int (rand (scalar @nouns))]
       . ""
       . $ prepositions [int (rand (scalar @prepositions))]
       . '.';
       $ story. = $ sentence;
   }
   print "\ n", $ story, "\ n";
   print "\ nType \" quit \ "to quit, or press Enter to continue:";
   $ input = <STDIN>;
}
#Here is a regular expression to match the string whose first character is q, if there is, then exit, / ^ is the starting position of the match, / i is not case sensitive
until ($ input = ~ / ^ \ s * q / i);
exit;
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.