Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: New to Perl: Hashes and int(rand())

by AnomalousMonk (Archbishop)
on Jun 14, 2014 at 10:14 UTC ( [id://1089884]=note: print w/replies, xml ) Need Help??


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
    I also thought about making it in a sub, and it is certainly a good idea, but I figured out that the OP just completed chapter 5 of Ovid's book and subroutines are explained only in chapter 7.

      I'm not sure I'd wait so long to introduce such a basic concept, but I haven't looked at Ovid's book and so I'm not familiar with its basic structure. Also, there's the little problem that I've never written a book of any kind...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1089884]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-20 00:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found