How to resolve the algorithm Dice game probabilities step by step in the Lua programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Dice game probabilities step by step in the Lua programming language

Table of Contents

Problem Statement

Two players have a set of dice each. The first player has nine dice with four faces each, with numbers one to four. The second player has six normal dice with six faces each, each face has the usual numbers from one to six. They roll their dice and sum the totals of the faces. The player with the highest total wins (it's a draw if the totals are the same). What's the probability of the first player beating the second player? Later the two players use a different set of dice each. Now the first player has five dice with ten faces each, and the second player has six dice with seven faces each. Now what's the probability of the first player beating the second player? This task was adapted from the Project Euler Problem n.205: https://projecteuler.net/problem=205

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Dice game probabilities step by step in the Lua programming language

Source code in the lua programming language

local function simu(ndice1, nsides1, ndice2, nsides2)
  local function roll(ndice, nsides)
    local result = 0;
    for i = 1, ndice do
      result = result + math.random(nsides)
    end
    return result
  end
  local wins, coms = 0, 1000000
  for i = 1, coms do
    local roll1 = roll(ndice1, nsides1)
    local roll2 = roll(ndice2, nsides2)
    if (roll1 > roll2) then
      wins = wins + 1
    end
  end
  print("simulated:  p1 = "..ndice1.."d"..nsides1..", p2 = "..ndice2.."d"..nsides2..",  prob = "..wins.." / "..coms.." = "..(wins/coms))
end

simu(9, 4, 6, 6)
simu(5, 10, 6, 7)


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)


  

You may also check:How to resolve the algorithm Combinations with repetitions step by step in the OCaml programming language
You may also check:How to resolve the algorithm Terminal control/Display an extended character step by step in the Haskell programming language
You may also check:How to resolve the algorithm Non-continuous subsequences step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Return multiple values step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Loops/For step by step in the COBOL programming language