in reply to What the other half does

I wrote a simple program in perl to generate character stats for Advanced Dungeons
And Dragons. It works for the most part, but could use some fine tuning. All of the die
rolls are somewhat on the high side, and not at all what actually rolling three six-sided dice
would produce. However, if you want some high end characters, its exactly what you need. :)

TStanley
There can be only one!

#!/usr/bin/perl use strict -w; sub Random () { my $randnum = int (rand(18))+1; return $randnum; } my $x=0; while ($x<=5) { @STR[$x]=Random(); @INT[$x]=Random(); @WIS[$x]=Random(); @DEX[$x]=Random(); @CON[$x]=Random(); @CHR[$x]=Random(); $x++; } print("Strength:\t\t"); my $x=0; while($x<=@STR) { print ("$STR[$x]\t"); $x++; } print("\n"); print("Intelligence:\t\t"); my $x=0; while($x<=@INT) { print ("$INT[$x]\t"); $x++; } print("\n"); print("Wisdom:\t\t\t"); my $x=0; while($x<=@WIS) { print ("$WIS[$x]\t"); $x++; } print("\n"); print("Dexterity:\t\t"); my $x=0; while($x<=@DEX) { print ("$DEX[$x]\t"); $x++; } print("\n"); print("Constitution:\t\t"); my $x=0; while($x<=@CON) { print ("$CON[$x]\t"); $x++; } print("\n"); print("Charisma:\t\t"); my $x=0; while($x<=@CHR) { print ("$CHR[$x]\t"); $x++; } print("\n");

Replies are listed 'Best First'.
Re: Re: What the other half does
by salvadors (Pilgrim) on Jan 05, 2001 at 17:33 UTC

    All of the die rolls are somewhat on the high side, and not at all what actually rolling three six-sided dice would produce.

    I don't really see how they come out high on average - all that happens is that they come out fairly evenly spread over the entire range, whereas you get more of a bell-curve of probability with 3x6-sided...

    In terms of "fine tuning", you should really turn some of that into loops. You're doing pretty much the same thing over and over again:

    foreach (qw/Strength Intelligence Wisdom Dexterity Constitution Charisma/) { printf "%-15s", $_; for (1 .. 6) { printf "%2d\t", int(rand(6)) + int(rand(6)) + int(rand(6)) + 3; } print "\n"; }

    Or:

    printf "%s\t%s\n", $_, join "\t", map { int(rand(6)) + int(rand(6)) + int(rand(6)) + 3 } (1 .. 6) for (qw/Str Int Wis Dex Con Chr/);

    Tony

      Thank you for your suggestion. I will definitely do that.

      TStanley
      There can be only one!
Re: Re: What the other half does
by stephen (Priest) on Jan 06, 2001 at 04:10 UTC
    Another way of doing the same thing:
    #!/usr/bin/perl use strict; ##################### Script Constants ###################### use constant CHARACTER_STATS => qw(Strength Intelligence Wisdom Dexter +ity Constitution Charisma); ################### Main Program ############################ MAIN: { my $character = make_character(); foreach my $stat (keys %$character) { print STDOUT ("$stat: \t\t", join("\t", @{ $character->{$stat} }), "\n" ); } } sub make_character { my $character = {}; for (my $x = 0; $x<=5; $x++) { foreach my $stat ( CHARACTER_STATS ) { push(@{$character->{$stat}}, Random()); } } return $character; } sub Random () { return (int (rand(18))+1); }
    If you're interested in having the dice follow a normal distribution, check out the Math::Random module.

    stephen