public int RollDice() { DiceRolls.Clear(); int result = 0; Random randy = new Random(); if (dieMax < 1) { return(1); } else { if (dieAmt > 999) { dieAmt = 999; } for (int i = 0; i < dieAmt; i++) { int temp = randy.Next(dieMax) + 1; DiceRolls.Add(temp); result += temp; } RawDieTotal = result; result += add; return(result); } }
/// <summary> /// 1. Get all dice rolls iteratively while also keeping track of sum and prod mod /// 2. filter the elements having equal sum and prod mod values. /// The running time is O(6^n) /// The space requriement is O(6^n) /// /// This is not an optimal solution as space requirement is more here as we are tackling the problem as BFS /// </summary> /// <param name="mod">mod value so that we dont have overflow for sum and product</param> /// <param name="maxDiceRolls">max number of dice rolls that needs to be performed</param> /// <returns></returns> public static int GetCountOfGoodDiceRolls(int mod, int maxDiceRolls) { //1. initialization List <DiceRolls> previous = new List <DiceRolls>(); List <DiceRolls> current = new List <DiceRolls>(); previous.Add(new DiceRolls(mod)); //2. get all the dice rolls for (int i = 1; i <= maxDiceRolls; i++) { foreach (DiceRolls dr in previous) { for (int diceVal = 1; diceVal <= 6; diceVal++) { DiceRolls drClone = dr.Clone(); drClone.Add(diceVal); current.Add(drClone); } } previous = current; current = new List <DiceRolls>(); } //3. Get all the good rolls return(previous.Where(x => x.ProdMod == x.SumMod).Count()); }