r/dailyprogrammer • • Sep 30 '12
[9/30/2012] Challenge #102 [easy] (Dice roller)
In tabletop role-playing games like Dungeons & Dragons, people use a system called dice notation to represent a combination of dice to be rolled to generate a random number. Dice rolls are of the form AdB (+/-) C, and are calculated like this:
- Generate A random numbers from 1 to B and add them together.
- Add or subtract the modifier, C.
If A is omitted, its value is 1; if (+/-)C is omitted, step 2 is skipped. That is, "d8" is equivalent to "1d8+0".
Write a function that takes a string like "10d6-2" or "d20+7" and generates a random number using this syntax.
Here's a hint on how to parse the strings, if you get stuck:
Split the string over 'd' first; if the left part is empty, A = 1,
otherwise, read it as an integer and assign it to A. Then determine
whether or not the second part contains a '+' or '-', etc.
48
Upvotes
4
u/prondose 0 0 Sep 30 '12 edited Oct 01 '12
Perl:
4
u/takac00 Sep 30 '12
Think this 3 liner works :D https://gist.github.com/3807542
4
u/tomasienrbc Oct 17 '12
python: my first program ever
3
u/skeeto -9 8 Oct 01 '12
In ANSI C,
3
u/[deleted] Oct 05 '12 edited Oct 05 '12
Sed/Awk:
2
u/Teraka Sep 30 '12
In Python, using a regex to interpret the input :
2
u/[deleted] Sep 30 '12
Tried my hand at using Dart.
Heres the general HTML file if you want to test:
And a screenshot:
http://i.imgur.com/Q3D8G.png
2
u/swarage 0 0 Sep 30 '12 edited Oct 30 '12
really bloated ruby code. I plan on shortening it and reuploading. http://codepad.org/I6QY0mx8 [edit] new code that's half as long (http://codepad.org/7fGyUoYu) [/edit] [edit2]http://codepad.org/Ak9Eq5f9[/edit2]
2
u/chaoticgeek Sep 30 '12
I'll toss my python attempt in. Not as pretty or efficent as the other two. But mine does take into account people may put spaces in to throw someone off. And it keeps you in the program until you give it a blank line.
2
u/[deleted] Sep 30 '12 edited Sep 30 '12
In C (:
2
u/railsandjs Sep 30 '12
Ruby
This is basically the first "program" I have written and I'd really appreciate any feedback on things I should have done completely different. I know it's probably the least fancy way of doing it. Thanks!
2
u/ill_will Oct 05 '12 edited Oct 05 '12
Python:
3
u/pivotallever Sep 30 '12 edited Sep 30 '12
python
output
3
u/Josso Sep 30 '12
Python:
Input:
Output:
3
u/ragnatic Sep 30 '12
My take in C#
3
u/skeeto -9 8 Sep 30 '12
In Emacs Lisp,
Example rolls:
2
u/Wedamm Sep 30 '12 edited Sep 30 '12
Haskell:
Usage:
Edit: Now obsolete: [[[
Because i use
tryfor the optional parts it don't catch all parse-errors anymore. Example:Has anyone an idea how to amend that? ]]]
I added
eof. This assures that any wrong input at the end would raise an error.Thanks to IceDane!
2
u/[deleted] Sep 30 '12
Still a bit inefficient, but it works.
Java:
Output:
1
u/the_nonameguy Sep 30 '12
Dusted off my python skills after 1 years with this boy, don't expect anything marvelous
https://gist.github.com/3806407
1
u/bschlief 0 0 Sep 30 '12
A ruby solution.
1
u/kewlar Sep 30 '12
PHP:
1
u/ixid 0 0 Sep 30 '12 edited Oct 03 '12
In D, first the elegant but slightly cheaty method:
Then a couple of others with less reliance on the library:
1
u/K1kuch1 Sep 30 '12
C
Ouput:
1
u/AgoAndAnon 0 1 Sep 30 '12
Ruby code golf:
1
u/skibo_ 0 0 Oct 01 '12
Suggestions, criticism, etc., are welcome feedback.
1
u/EvanHahn Oct 01 '12
My first Go code (simulate here):
1
u/Shuik Oct 01 '12
In C++. I'm quite new to programming.
1
u/spacemoses 1 1 Oct 02 '12 edited Oct 02 '12
Lua: Function to parse the string into a die object, and a function to roll a die object.
1
u/PoppySeedPlehzr 1 0 Oct 02 '12
Python
1
u/[deleted] Oct 02 '12 edited Oct 02 '12
Ugly C
1
u/lux_romana Oct 02 '12
Yet another solution in JavaScript:
1
u/Say_What1 Oct 03 '12
Python:
Output:
Still really new at Python, any feedback would be appreciated.
1
u/nagasgura 0 0 Oct 03 '12 edited Oct 05 '12
Python:
Output:
1
u/jecxjo Oct 04 '12
Common Lisp:
Usage:
1
u/srp10 Oct 04 '12
Just starting out with Python here...
1
u/tagus Oct 05 '12
C++:
1
u/robin-gvx 0 2 Oct 05 '12
I made this command line utility a while ago in Python:
https://dl.dropbox.com/u/2000007/dice.tar.gz
EDIT: there is no license mentioned in the archive file, but I released a .deb of it under the X11/MIT license.
1
u/rickster88 Oct 06 '12
Ruby:
1
u/codemac Oct 07 '12
Scheme (guile):
1
u/jboyle89 0 0 Oct 08 '12
C++:
Using this function:
1
u/PenguinKenny Oct 08 '12 edited Oct 08 '12
C# (still fairly new)
1
u/[deleted] Oct 09 '12
1
u/elemental_1_1 Oct 10 '12
Saw this today, did 75 lines in Java. How could I stop repeating myself so much? I couldn't get OR logical operator to work properly in regex.
1
u/CFrostMage Oct 10 '12 edited Oct 10 '12
PHP:
1
u/Die-Nacht 0 0 Oct 11 '12
Python, without regex:
1
u/[deleted] Oct 12 '12 edited Oct 13 '12
C++:
Usage:
1
u/[deleted] Oct 14 '12
C++. Any input is appreciated.
1
u/clojure-newbie Oct 17 '12
Clojure:
Usage:
1
u/mortisdeus Oct 20 '12
Python:
1
u/alexandream 0 0 Oct 23 '12 edited Oct 23 '12
This is mine in racket, but I think it's too big for what it does :(
1
u/[deleted] Oct 30 '12 edited Oct 30 '12
Python 2.7
1
u/Davorak Nov 28 '12
After reading the problem I thought for a moment about how I would create this functionality if I needed it on the spot.
Haskell's infix operators seem to fit the bill. Unfortuantly the character 'd' can not be an infix operator. Surrounding a function with backticks turns it into an infix operator so
dcould work, but it seemed like too much work to type 2d5.On Mac OS X option char produces produces characters outside the standard ASCII set and option-d happens to produce '∂' which close enough looking to d and will act as a infix operator for Haskell.
So in the repl:
Most of the desired functionality with little work
1
u/marekkpie Jan 22 '13
Lua. Lua has a subset of regex call patterns, and while its not as powerful as many other languages, it's been plenty powerful so far.
1
u/robbieferrero Sep 30 '12
Javascript: This is my first time in this subreddit. Cheers. (also, I know that new Function isn't the most elegant or performant way to do it, but I wrote this in about five minutes)
1
u/kirsybuu 0 1 Sep 30 '12
D
Example:
1
u/[deleted] Oct 01 '12 edited Oct 01 '12
Gave it a quick go in Java.
It's not exactly flexible but it works. Gonna make it a little nicer after I've had some sleep.
-1
u/hamishiam 1 0 Oct 06 '12
A Java solution that is actually Object Oriented (ohmygosh!) Consequently it's probably the longest submission, though potentially also one of the most efficient because uses a hand-rolled LL1 parser, rather than regular expression, which are evil.
https://gist.github.com/3844967
Edited: spelling
0
u/meowfreeze Oct 01 '12 edited Oct 04 '12
Python.
Output:
0
u/speakingcode Oct 01 '12 edited Oct 01 '12
Here's a Java solution without much use of regex or crytpic string manipulation . It isn't as concise as some solutions, but fairly readable, reusable, all that good stuff. also it is robust - it will print a usage message and exit cleanly when it detects invalid input. works from cmd line...
0
u/whangarei Oct 07 '12 edited Oct 07 '12
python:
-1
u/Riddlerforce Sep 30 '12
You can take a shortcut by multiplying A*B. and generating random numbers in the range of 1 to A*B.
To account for the normal distribution you would get from actually rolling A number of dice, multiplying A*B, and generate random numbers in a normal distribution with the middle of the bell-curve being A*B/2.
-1
u/iMalevolence Sep 30 '12
Need to work on efficiency, but it gets the job done I think.
Java:
0
u/[deleted] Oct 01 '12
[deleted] — view deleted comment
0
u/[deleted] Oct 01 '12
[deleted] — view deleted comment
0
u/[deleted] Sep 30 '12
[deleted] — view deleted comment
0
u/[deleted] Sep 30 '12
[deleted] — view deleted comment