Ruby Quiz wrote:
> The three rules of Ruby Quiz:
> 
> 1.  Please do not post any solutions or spoiler discussion for this quiz until
> 48 hours have passed from the time on this message.
> 
> 2.  Support Ruby Quiz by submitting ideas as often as you can:
> 
> http://www.rubyquiz.com/
> 
> 3.  Enjoy!
> 
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> 
> by Matthew D Moss
> 
> Time to release your inner nerd.
> 
> The task for this Ruby Quiz is to write a dice roller. You should write a
> program that takes two arguments: a dice expression followed by the number of
> times to roll it (being optional, with a default of 1). So to calculate those
> stats for your AD&D character, you would do this:
> 
> 	> roll.rb "3d6" 6
> 	72  64  113  33  78  82
> 
> Or, for something more complicated:
> 
> 	> roll.rb "(5d5-4)d(16/d4)+3"
> 	31
> 

My submission isn't going to win points for brevity - at 600+ lines it's
maybe a bit long to post here.

It's got a few extras in there, though:

$ ./dice.rb "(5d5-4)d(16/d4)+3"
45

$ ./dice.rb "3d6" 6
11  7  10  13  9  14

$ ./dice.rb -dist "2d5 + 1dd12"
Distribution:
3	0.0103440355940356
4	0.0276987734487735
5	0.0503975468975469
6	0.0773292448292448
7	0.107660533910534
8	0.120036676286676
9	0.120568783068783
10	0.112113997113997
11	0.096477873977874
12	0.07495670995671
13	0.0588945406445407
14	0.0457661135161135
15	0.0345793650793651
16	0.0250565175565176
17	0.0171049783549784
18	0.0107247474747475
19	0.00596632996632997
20	0.00290909090909091
21	0.00113636363636364
22	0.000277777777777778
Check total: 1.0
Mean 9.75  std. dev 3.37782803325187

$ ./dice.rb -cheat "2d5 + 1dd12" 19
19	: D5=2 D5=5 D12=12 D12=12  p=0.000277777777777778

$ ./dice.rb -cheat "2d5 + 1dd12" 25
Cannot get 25

I've shoved it on http://homepage.ntlworld.com/a.mcguinness/files/dice.rb

-- 
Andrew McGuinness
http://anomalyuk.blogspot.com/