in reply to New to Perl: Hashes and int(rand())
The next steps are to abstract the process of rolling a 6-sided die, and then to abstract the process of creating an n-sided die to roll. The latter step is a bit advanced, but just be aware it's possible. Also be aware that the built-in rand function may not be all that random.
c:\@Work\Perl>perl -wMstrict -le "printf qq{%d }, die6() for 1 .. 10; print ''; ;; sub die6 { return 1 + int rand 6; } ;; ;; sub make_die { my ($sides) = @_; ;; return sub { return 1 + int rand $sides; }; } ;; my $die_20 = make_die(20); my $die_5 = make_die(5); ;; printf qq{20: %d; 5: %d \n}, $die_20->(), $die_5->() for 1 .. 6; " 1 5 4 4 6 5 4 1 1 1 20: 9; 5: 2 20: 13; 5: 1 20: 6; 5: 1 20: 9; 5: 3 20: 9; 5: 1 20: 5; 5: 1
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: New to Perl: Hashes and int(rand())
by Laurent_R (Canon) on Jun 15, 2014 at 09:20 UTC | |
by AnomalousMonk (Archbishop) on Jun 15, 2014 at 13:04 UTC |