Skip to content

Instantly share code, notes, and snippets.

@anna-is-cute
Last active December 27, 2015 18:54
Show Gist options
  • Save anna-is-cute/1bdf8aa4c5543787dc16 to your computer and use it in GitHub Desktop.
Save anna-is-cute/1bdf8aa4c5543787dc16 to your computer and use it in GitHub Desktop.
import java.util.Random
class Roll {
private val random = Random()
fun roll(n: Int = 1, d: Int = 6): String {
val res = (1..n).map { this@Roll.random.nextInt(d) + 1 }
return "${res.joinToString()} = ${res.sum()}"
}
fun roll(input: String): String {
val (n, d) = input.split("d").map { it.toInt() }
return roll(n = n, d = d)
}
}
from random import randrange
def roll(n=1, d=6):
if type(n) == str:
return roll(*[int(x) for x in n.split('d')])
res = [randrange(1, d + 1) for i in range(n)]
return '{} = {}'.format(', '.join([str(x) for x in res]), sum(res))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment