[ create a new paste ] login | about

Link: http://codepad.org/7fGyUoYu    [ raw code | output | fork ]

Ruby, pasted on Sep 30:
def diceroll(str)
	wstr = str.split(//)
	a,b,c = 0,0,0
	if wstr[0] == "d"
		a = 1 
	else
		a = (wstr[0..wstr.index("d")-1]).join.to_i
	end
	if wstr.index("+") != nil
		b = ((wstr[(wstr.index("d")+1)..(wstr.index("+"))]).join).to_i
		c = (wstr[wstr.index("+")..wstr.length-1]).join.to_i

	elsif wstr.index("-") != nil
		b = ((wstr[(wstr.index("d")+1)..(wstr.index("-"))]).join).to_i
		c = (wstr[wstr.index("-")..wstr.length-1]).join.to_i
	else
		b = (wstr[(wstr.index("d")+1)..(wstr.length-1)].join).to_i
	end
	loop do
		constantvar = rand(b)
		return constantvar+c if constantvar >= a
	end
end

puts diceroll("0d10+5")


Output:
1
6


Create a new paste based on this one


Comments: