Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Dungeons and Dragons die roller (Golf)

by Grimy (Pilgrim)
on Oct 25, 2016 at 16:06 UTC ( [id://1174699]=note: print w/replies, xml ) Need Help??


in reply to Dungeons and Dragons die roller (Golf)

Straightforward 39 bytes (37 + 2 flags) solution :
#!perl -pl /\d+\+/;$-+=rand$&for 1..$_;$_+=$'+$-
More complex 35 bytes solution :
#!perl -pl s/d/q(+~~rand($')+d)x$`/e;$_=eval
Due to the use of $', they can’t easily be ran from the command-line. Just store them in a file.

Replies are listed 'Best First'.
Re^2: Dungeons and Dragons die roller (Golf)
by Anonymous Monk on Nov 04, 2016 at 18:51 UTC
    After staring at this for a while, I'm not sure how exactly this works (mainly, what does +~~ do?). Would it be possible to explain how the 35 bytes solution works?

      ~~rand($') is the same as int(rand($'))

        I may be missing something very obvious, but could someone step through this one step at a time? The way I'm reading it, something like the below happens, and it doesn't make much sense to me.
        Input: 2d4+10 Code: s/d/q(+~~rand($')+d)x$`/e; 2d4+10 2(q(+~~rand($')+d)x$`)4+10 2(q(+int(rand($'))+d)x$`)4+10 2(q(+int(rand(4+10))+d)x2)4+10 2('+int(rand(4+10))+d'x2)4+10 2('+int(rand(4+10))+d''+int(rand(4+10))+d')4+10 =?????

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-19 17:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found