pastebin
| #1 paste tool since 2002
create new paste
tools
api
archive
real-time
faq
pastebin
Follow @pastebin
create new paste
trending pastes
sign up
login
my settings
my profile
My Pastes
Public Pastes
Untitled
12 sec ago
Untitled
25 sec ago
“public stati...
25 sec ago
The Grid 456 1992 @ 320...
25 sec ago
Press Release March 31s...
34 sec ago
Does this work?
Java | 31 sec ago
Crocko Premium Accounts...
35 sec ago
Untitled
37 sec ago
Layout Width
Share Pastebin
Untitled
By: a guest | Apr 24th, 2008 | Syntax:
Groovy
| Size: 0.52 KB | Hits: 142 | Expires: Never
Download
|
Raw
|
Embed
|
Report abuse
import
java.util.Random
def
diceString
(
5.
0
%
2Fdocs
%
2Fapi
%
2F">
String
s
)
{
s.
replaceAll
(
"(
\\
d+)d(
\\
d+)"
,
"
\$
1.d(
\$
2)"
)
}
class
DiceRoller
{
def
private
static
generator
=
new
5.
0
%
2Fdocs
%
2Fapi
%
2F">
Random
(
)
public
static
5.
0
%
2Fdocs
%
2Fapi
%
2F">
Integer
d
(
5.
0
%
2Fdocs
%
2Fapi
%
2F">
Integer
self,
5.
0
%
2Fdocs
%
2Fapi
%
2F">
Integer
facets
)
{
def
results
=
[
]
for
(
i
in
(
1..
self
)
)
{
results
<<
(
generator.
nextInt
(
facets
)
+
1
)
}
println
"$results"
return
results.
inject
(
0
)
{
sum, value
->
sum
+=
value
}
}
}
use
(
DiceRoller
)
{
Eval.
me
(
diceString
(
"3d6 + 3"
)
)
}
create new paste
|
create new version of this paste
RAW Paste Data
import java.util.Random def diceString(String s) { s.replaceAll("(\\d+)d(\\d+)", "\$1.d(\$2)") } class DiceRoller { def private static generator = new Random() public static Integer d(Integer self, Integer facets) { def results = [] for( i in (1..self)) { results << (generator.nextInt(facets) + 1) } println "$results" return results.inject(0) { sum, value -> sum += value } } } use(DiceRoller) { Eval.me(diceString("3d6 + 3")) }