Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: Dungeons and Dragons die roller (Golf)

by tybalt89 (Monsignor)
on Nov 05, 2016 at 02:30 UTC ( #1175344=note: print w/replies, xml ) Need Help??


in reply to Re^2: Dungeons and Dragons die roller (Golf)
in thread Dungeons and Dragons die roller (Golf)

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

  • Comment on Re^3: Dungeons and Dragons die roller (Golf)

Replies are listed 'Best First'.
Re^4: Dungeons and Dragons die roller (Golf)
by Anonymous Monk on Nov 07, 2016 at 17:00 UTC
    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 =?????
      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 2+int(rand(4))+d+int(rand(4))+d4+10 # or looking at each term 2 # since rand return 0..N-1 instead on 1..N, we add one f +or every die +int(rand(4)) # int in 0..3 +d # 0 +int(rand(4)) # int in 0..3 +d4 # also 0, the 'd' are here to help ignore the 4 +10 # desired extra # then eval that :)
        Awesome, thank you! With a little bit of experimenting now, I found that rand(4+10) would pick 0...13, but rand('4+10') picks 0...3, ignoring everything beyond the first string of digits. I didn't realize that odd behavior, nor did I realize you could make something like 'd4', which is ignored when evaluating the statement, instead of causing an error. Those seem like odd loopholes in the way Perl works, but that just makes the whole command that much more beautiful. Thanks again!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2023-12-04 15:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (25 votes). Check out past polls.

    Notices?