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

Hm. You construct your arrays all fine, but after they are constructed, the random() function isn't called when you reference an array item, but you absolutely need runtime randomness for your layout to work.

You have to add a bit of magic to your arrays with tie.

use Tie::Array; @ISA = qw(Tie::StdArray); sub FETCH { my ($self,$index) = @_; if (ref($self->[$index]) =~ /CODE/) { return $self->[$index]->(); } $self->[$index]; }

Now convert all your array elements which are dynamically constructed (i.e via concatenation of some strings and calls to e.g. random()) into anonymous subs, and tie those arrays:

my @MagicItems; tie @MagicItems, main; # main, or the package of your +code @MagicItems = ( "can use all magic items", "can not use any magic items", sub { "can only use magic items in the ".random("MITp")." group"}, sub { "can not use magic items in the ".random("MITp")." group"}, sub { "causes $MagicItemGroup[rand @MagicItemGroup] to dysfunction + $radius"}, sub { "causes $MagicItemGroup[rand @MagicItemGroup] to loose their + power $radius"}, sub { "attracts $MagicItemGroup[rand @MagicItemGroup] $radius"}, sub { "repels $MagicItemGroup[rand @MagicItemGroup] $radius"}, sub { "destroys $MagicItemGroup[rand @MagicItemGroup] $radius"}, );

Do this with every array which has randomly constructed items. Then

for (1..10) { print $MagicItems[rand(@MagicItems,)],"\n"; };

will yield e.g.

can not use any magic items can not use magic items in the Magical Liquids group can not use magic items in the Musical Instruments group destroys magic items in the Scrolls group in a 20' radius repels magic items in the Bags & Bottles group in a 20' radius causes all magic items to loose their power in a 20' radius can not use magic items in the Rods group causes magic items in the Humorous Items group to loose their power in + a 20' radius can only use magic items in the Weird Stuff group can only use magic items in the Humorous Items group

Solving the static radius problem is left as an exercise to the reader.

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^3: Creating a random generator
by Lady_Aleena (Priest) on Oct 05, 2007 at 07:08 UTC

    I think you, ikegami, and Petruchio did great work for me during that Chatterbox conversation yesterday. It pulled what you did here together with their ideas.

    Many thanks for what you three did!

    Lady Aleena

    "An it harm none, do as ye will."