dice roll to add total to array lua

 local function comp(ndice1, nsides1, ndice2, nsides2)  local function throws(ndice, nsides)    local sums = {}    for i = 1, ndice*nsides do      sums[i] = 0    end    local function throw(ndice, nsides, s)      if (ndice==0) then        sums[s] = sums[s] + 1      else        for i = 1, nsides do          throw(ndice-1, nsides, s+i)        end      end      return sums    end    return throw(ndice, nsides, 0)  end  local p1 = throws(ndice1, nsides1)  local p2 = throws(ndice2, nsides2)  local wins, coms = 0, nsides1^ndice1 * nsides2^ndice2  for k1,v1 in pairs(p1) do    for k2,v2 in pairs(p2) do      if (k1 > k2) then        wins = wins + v1 * v2      end    end  end  print("computed:  p1 = "..ndice1.."d"..nsides1..", p2 = "..ndice2.."d"..nsides2..", prob = "..wins.." / "..coms.." = "..(wins/coms))end comp(9, 4, 6, 6)comp(5, 10, 6, 7) 

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source