Page 1 of 1

[SOLVED] Trying to roll X number of Y sided dice

PostPosted: Fri Apr 30, 2021 11:58 pm
by Galarandir
I'm new to coding and stuck on a problem I can't find an answer for.

I'm trying to put together something that will allow me to input into two cells a number of dice to be rolled and the number of sides each die has that will give me the sum of the dice rolled in a third cell.
dicesample.png
dicesample.png (4.27 KiB) Viewed 1106 times
https´╝Ü//i.ibb.co/0XMfqq9/dicesample.png

Re: Trying to roll X number of Y sided dice

PostPosted: Sat May 01, 2021 6:20 am
by RusselB
Welcome to the Forums.
There is no way to calculate the sum of the rolled dice, as there is no way of knowing which die is going to land on which side.
It is possible to calculate the range of possible results. Your minimum would be simply the number of dice. Your maximum would be the number of sides times the number of dice.
Please note that the above calculations are based on the standard die sequencing. ie: starting at 1 and increasing by 1 per side.

P.S.: You could get a total by calculating a random roll for each die and then summing those up. I can think of two ways of doing that, though both have the limitation of setting a maximum number of die for the calculations. Since you have an entry for the number of die, you would have to either limit that or find another way to calculate an unknown number of die.
This might be possible with a macro, as it would have to contain a loop, but (in Calc) I can think of no other options.

Re: Trying to roll X number of Y sided dice

PostPosted: Sat May 01, 2021 9:30 am
by Zizi64
You must generate random integer numbers between the minimum and maximum value of one dice (it is possible by usage the Cell Formulas), but you must do it many times cyclically by number of the dices.
Therefore the easiest way to solve this task the macro function. Write your macro function - if you need it really.


Otherwise you must use helper cells what handle all of the dices (the possible maximum count of the dices), and then you must sum the those dice values (only) what are inside the actual dice count. A Ctrl-Shift-F9 will "roll the dices" again.

Here is a sample file. Check the applied Named Ranges, the applied Formulas, the applied Data Validity in the input cells, the applied Styles and the Conditional Format feature.

Note: my sample file was created in the LibreOffice 6.1.6 version. Some features work differently than they work in the Apache OpenOffice. I just tried it in the AOO 4.1.3 portable version: it works.

DiceSum.ods
(14.73 KiB) Downloaded 39 times


 Edit: Edited: I just fixed some glitches: 


DiceSum_fixed.ods
(13.86 KiB) Downloaded 37 times

Re: Trying to roll X number of Y sided dice

PostPosted: Sat May 01, 2021 10:05 am
by F3K Total
Hello,
found a solution without helper-cells, using array formulas.

Re: Trying to roll X number of Y sided dice

PostPosted: Sat May 01, 2021 10:37 am
by Zizi64
@F3K Total :
Very nice solution.

Just a small note.
It requires some "helper" cells: A1:A30 (or less/more). (But you can use them for storing other things.)

Re: [SOLVED]Trying to roll X number of Y sided dice

PostPosted: Mon May 03, 2021 8:44 am
by Galarandir
Thank you for your help