Skip to content

Instantly share code, notes, and snippets.

@mudhairless
Created November 24, 2013 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mudhairless/7633637 to your computer and use it in GitHub Desktop.
Save mudhairless/7633637 to your computer and use it in GitHub Desktop.
'Roll a die or more, DND style syntax.
declare function roll( byref s as string ) as uinteger
#define ALGO_CRYPTO 5
#define ALGO_QBASIC 4
#define ALGO_MERSEN 3
#define ALGO_FAST 2
#define ALGO_CRAND 1
'Main
var algo = valint(environ("RALGO"))
if algo = 0 then algo = 5
if algo > 5 then algo = 5
if command() = "--test" then
print "Don't trust random numbers?"
print "Here is a distribution breakdown of 100,000 random d20 rolls."
print "*note the results are divided by 10 for display"
print "Algorithm in use: (see help to decode) " & algo
print
randomize timer, algo
dim dist(1 to 20) as integer
for n as integer = 0 to 99999 : dist( roll("d20") ) += 1 : next
for n as integer = 1 to 20 : print using "### "; dist(n)\10; : next
print
for n as integer = 1 to 20 : print using " ## "; n; : next
print
elseif command() <> "" andalso command() <> "--help" then
randomize timer, algo
for n as integer = 1 to __FB_ARGC__ -1
print command(n) & ": " & roll(command(n))
next
else
print "Simulate a dice roll using standard DND format."
print "This die roller is Open Source under the STFUOIWCU License."
print
print "Usage:"
print "rollin {option} die (repeated die may be seperated by spaces)"
print
print "Options:"
print "--test - run a distribution test of the pRNG."
print "--help - print this help message."
print
print "Environment Variables Recognized:"
print "RALGO - Set the pRNG used to generate the dice rolls."
print " Is a nummber between 1 and 5 with 5 being the default."
print " Algorithms:"
print " 1 - C rand() function"
print " 2 - 'fast' function"
print " 3 - Mersenne Twister, reasonably good"
print " 4 - the QBASIC rnd function"
print " 5 - Windows/Linux crypto grade pRNG"
print
print "Die Specification Format: {x}d[n]{+z}"
print "where optional x is the number of die in this roll"
print "the letter d is required"
print "n is the number of faces on the die, a standard die is 6"
print "+z is an optional bonus (preceded by a +) to be applied"
print " after all rolls."
print
print "Example dice rolls:"
print "2d6 - Roll 2 standard dice, result will be"
print " at least 2 and at a maximum 12."
print "3d12+4 - Roll 3x 12 sided dice, result will be"
print " at least 7 (3+4) and at most 40 (36+4)."
print
print "Example usage:"
print "rollin 2d6+1 1d20+5 d100+2 d20"
print "2d6+1: " & roll("2d6+1")
print "1d20+5: " & roll("1d20+5")
print "d100+2: " & roll("d100+2")
print "d20: " & roll("d20")
end if
function roll( byref s as string ) as uinteger
var d = instr(s,"d")
var plus = instr(s,"+")
var ret = 0u
var rep = 0u
if d = 1 then
rep = 1
else
rep = valint(mid(s,1,d-1))
end if
var die = valint(mid(s,d+1,plus-d-1))
var bonus = valint(mid(s,plus+1))
for n as integer = 1 to rep
ret += int(rnd(1)*die)+1
next
return ret + bonus
end function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment