in reply to Re^2: Creating a random generator
in thread Creating a random generator

The first 3 scalars are default values. If they are not declared differently in the array, then they don't change. If they are declared in the array, then they change to the new value.

Also, all variables have to be as small as possible. I try to conserve space as much as possible, hence the one and two letter variable names.

In the array, the name of the monster will be chosen randomly, but the other information such as which book it comes from and what page it is on must be declared, hopefully, within the array. If I have to do separate hashes(?) for each value, it will be way to big. Other random generators with similar variables have up to 10 different variables changed all on one line.

Hopefully that made sense.

There is a random generation program that I use for what I am trying to recreate in Perl called TableSmith. If you know of it, that will show you where I am coming from.

Replies are listed 'Best First'.
Re^4: Creating a random generator
by wfsp (Abbot) on Sep 08, 2007 at 09:06 UTC
    I find a lookup table is the way to go for these situations.
    #!/usr/bin/perl use strict; use warnings; my $beholder_default = 'beholder'; my $param = get_param(); my @keys = ( 'beholder', 'deathkiss', 'eye of the deep', 'gauth', 'spectator', 'undead', 'hive mother', 'director', 'examiner', 'lensman', 'watcher' ); my %lookup = ( 'beholder' => { book => 'Monstrous Manual', beholder => 'beholder', page => 21, }, 'death kiss' => { book => 'Monstrous Manual', beholder => 'beholder', page => 21, }, 'eye of the deep' => { book => 'Monstrous Manual', beholder => 'beholder', page => 21, }, 'gauth' => { book => 'Monstrous Manual', beholder => 'beholder', page => 21, }, 'spectator' => { book => 'Monstrous Manual', beholder => 'beholder', page => 21, }, 'undead' => { book => 'Monstrous Manual', beholder => 'beholder', page => 21, }, 'hive mother' => { book => 'Monstrous Manual', beholder => 'beholder', page => 21, }, 'director' => { book => 'Monstrous Manual', beholder => 'beholder', page => 21, }, 'examiner' => { book => 'Monstrous Manual', beholder => 'beholder', page => 21, }, 'lensman' => { book => 'Monstrous Manual', beholder => 'beholder', page => 25, }, 'overseer' => { book => 'Monstrous Manual', beholder => 'beholder', page => 25, }, 'watcher' => { book => 'Monstrous Manual', beholder => 'beholder', page => 25, }, ); my $key; if ($param){ $key = $beholder_default; } else{ $key = $keys[rand @keys]; } my $beholder = $lookup{$key}{beholder}; my $book = $lookup{$key}{book}; my $page = $lookup{$key}{page}; print "beholder: $beholder\n"; print "book: $book\n"; print "page: $page\n"; sub get_param { # get your input from somewhere my $param = undef; return $param; }
    Ah, I didn't see your update before I posted this.

      To give you all an idea of what the output is to look like, here is each item of the array.

      • beholder (Monstrous Manual, pg 21)
      • death kiss beholder (Monstrous Manual, pg 21)
      • eye of the deep beholder (Monstrous Manual, pg 21)
      • gauth beholder (Monstrous Manual, pg 21)
      • spectator beholder (Monstrous Manual, pg 21)
      • undead beholder (Monstrous Manual, pg 21)
      • hive mother beholder (Monstrous Manual, pg 25)
      • director beholder (Monstrous Manual, pg 25)
      • examiner beholder (Monstrous Manual, pg 25)
      • lensman beholder (Monstrous Manual, pg 25)
      • overseer beholder (Monstrous Manual, pg 25)
      • watcher beholder (Monstrous Manual, pg 25)
        I've reduce the size of the table for brevity.

        #!/usr/bin/perl use strict; use warnings; my $beholder_default = 'beholder'; my $param = get_param(); my %monster_lookup = ( 'beholder' => { book => 'Monstrous Manual', beholder => 1, page => 21, }, 'death kiss' => { book => 'Monstrous Manual', beholder => 0, page => 21, }, ); # this saves duplicating data in your code my @monsters = keys %monster_lookup; my $monster; if ($param){ $monster = $beholder_default; } else{ my $rand = int rand @monsters; $monster = $monsters[$rand]; } my $book = $monster_lookup{$monster}{book}; my $page = $monster_lookup{$monster}{page}; my $beholder = $monster_lookup{$monster}{beholder}; if (not $beholder){ print "$monster "; } print "beholder ($book, pg $page)\n"; sub get_param { # get your input from somewhere my $param = 0; return $param; }