Skip to content

Instantly share code, notes, and snippets.

@mike-pete
Created May 31, 2022 23:46
Show Gist options
  • Save mike-pete/e1ea73d69753c33d0f1b74c20f2b1ea9 to your computer and use it in GitHub Desktop.
Save mike-pete/e1ea73d69753c33d0f1b74c20f2b1ea9 to your computer and use it in GitHub Desktop.
D&D Stat Roller
  1. Roll a 6 sided die 4 times.
  2. Remove the lowest dice result.
  3. Add up the remaining numbers to get an ability score.
  4. Write down this ability score on note paper.
  5. Repeat these steps until you have 6 ability scores.
const rollD6 = () => Math.ceil(Math.random() * 6)
const getOneStat = () => {
let sumOfRolls = 0
let smallestRoll = 6
// roll 4 times and add the totals together
for (let i=0; i < 4; i++){
const roll = rollD6()
sumOfRolls += roll
smallestRoll = roll < smallestRoll ? roll : smallestRoll
}
// drop the smallest roll
sumOfRolls -= smallestRoll
return sumOfRolls
}
const getAllStats = () => {
let allRolls = []
// roll 6 stats
for (let i=0; i<6; i++){
allRolls[i] = getOneStat()
}
// sort largest to smallest
return allRolls.sort((a,b)=>b-a)
}
console.log(getAllStats())
import random
def oneStat():
sumOfRolls = 0
smallestRoll = 6
# roll 4 times and add the totals together
for i in range(4):
d6Roll = random.randint(1,6)
sumOfRolls += d6Roll
if d6Roll < smallestRoll:
smallestRoll = d6Roll
# drop smallest roll
sumOfRolls -= smallestRoll
return sumOfRolls
def allStats():
allRolls = []
# roll 6 stats
for i in range(6):
allRolls.append(oneStat())
# sort largest to smallest
allRolls.sort(reverse=True)
return allRolls
print(allStats())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment