I'm not a very experienced in TI-BASIC (~6 days of experience), and I was wondering what the fastest way is to take a list of numbers representing dice (each number represents the number of sides on each die), and return a list; each item on the returned list would be equal to how many times the sum of the rolled dice was the index of that item, taking into account every possible combination of die rolls.

Here's an example:

Code:
`{4,4,6}`
would result in
Code:
`{0,0,1,3,6,10,13,15,15,13,10,6,3,1}`

Out of the 96(4*4*6) possible combinations, 1 added up to 3, 3 added up to 4, 6 added up to 5, etc.

I have a program that works, but it is very slow. if you enter just 2d100, it will take a VERY long time to finish, as it has to go through my recursion loop a little over 10,000 times.
Here is my implementation, which may or may not be faster than yours. L₁ is where the list input is stored, and L₂ is where the output will be. L₃ is used.

Code:
```ClrList L₂,L₃ sum(L₁→dim(L₂ 1→L₃(1 For(I,1,dim(L₁ AnsL₁(I→L₃(I+1 End For(I,0,Ans-1 dim(L₁ Ans+Σ(remainder(int(I/L₃(J)),L₁(J)),J,1,Ans 1+L₂(Ans→L₂(Ans End ```

(Σ( can be replaced with sum(seq( and remainder(A,B) can be replaced with BfPart(A/B))

Maybe you could use the fact that the output is always symmetrical to optimize.
That timeframe sounds about right. It would probably be a more efficient use of your time to do it algebraically, and also, why are you making a program that does that? It also could be faster to do a Monte Carlo simulation, where the list updates in real time and the program runs as long as you want.
Although he never actually posted it in the topic, commandblockguy posted this extremely in-depth look at exactly this question: https://www.lucamoroni.it/the-dice-roll-sum-problem/
Yeah, I probably should have posted that here earlier, because it goes into quite a bit of detail about how to do the type of combinatorics that would be useful here.

Unfortunately it's not solving exactly the same question that was asked here - it's a less general version that assumes that all dice have the same number of sides. You might still be able to use it as an optimization that e.g. counts the number of occurrences of each side count, though.
To achieve what you want, you can use nested loops to go through all possible combinations of die rolls. Inside the loops, you can calculate the sum and use it as an index to increase the corresponding item in the resulting list. Don't forget to handle extreme cases, such as empty lists or cubes with zero edges. You can also use the virtual D20 cube at https://flipsimu.com/dice-roller/roll-d20/ to learn how the simulation works better. Keep experimenting, and you'll quickly become more proficient.
I had watched a video about doing this exact thing in O(n²) time (and storage)

Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

»
» All times are UTC - 5 Hours

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum