dice notation in excel

2019-05-31 06:49发布

i'm having an issue in excel and i need help from someone who knows better.

Quick story : i'm a rolist, i'm doing character sheet on excel and i need to do dice notation calcul.

I need to be able to write in a cell something like 3d6+2d4+6 or 3d8+2+3d9-2d6 or any combinaison. and then calculate the result in a other cell.

something like Rolldice(A1) and it would calculate it (if 3d8+2+4d8 was given it would give a number between 9 and 58)

Basicaly 1d6 means 1 dice with six faces, thus a random number betwen 1 and 6. 2d6 would mean doing 1d6 + 1d6 (thus a number between 2 and 12)

I don't mind if the result requires me to set the dices between braquets as in : (3d6)+2+(4d8)

it should be able to add, substract or multiply dices

1dX + 1dX or 1dX - 1dX or 1dX * 1dX

I would be immensly thankfull to the one who manage to do that.

I hope i made myself clear enough. feel free to ask specifications if needed.

Best regards,

ps: thanks for fixing my lack of good english. it's especially great to see the reviews and learn from it !

标签: excel dice
2条回答
Evening l夕情丶
2楼-- · 2019-05-31 07:42

Say we have dice expressions in column A

Hi-light the cells and run this small macro:

Sub DiceXlator()
    Dim r As Range, v As String, NewForm As String, deemode As Boolean
    Dim dee As String
    dee = "d"
    deemode = False
    For Each r In Selection
        v = r.Value
        NewForm = "="
        For i = 1 To Len(v)
            ch = Mid(v, i, 1)
            If ch = dee Then
                NewForm = NewForm & "*RANDBETWEEN(1,"
                deemode = True
            Else
                If Not IsNumeric(ch) And deemode Then
                    deemode = False
                    NewForm = NewForm & ")"
                End If
                NewForm = NewForm & ch
            End If
        Next i

        If deemode Then
            NewForm = NewForm & ")"
        End If

        r.Offset(0, 1).Formula = NewForm
    Next r
End Sub

The macro translates each dice expression into a standard Excel formula and places the formula in the adjacent cell in column B

Here is a tiny example of inputs/outputs

sample

EDIT#1:

Here is the same logic in the form of a User Defined Function - UDF

Public Function RollDice(r As Range) As Variant
    Application.Volatile
    Dim v As String, NewForm As String, deemode As Boolean
    Dim dee As String
    dee = "d"
    deemode = False
        v = r.Value
        NewForm = "="
        For i = 1 To Len(v)
            ch = Mid(v, i, 1)
            If ch = dee Then
                NewForm = NewForm & "*RANDBETWEEN(1,"
                deemode = True
            Else
                If Not IsNumeric(ch) And deemode Then
                    deemode = False
                    NewForm = NewForm & ")"
                End If
                NewForm = NewForm & ch
            End If
        Next i

        If deemode Then
            NewForm = NewForm & ")"
        End If

        RollDice = Evaluate(NewForm)

End Function

EDIT#2:

It is possible to get Min, Max, and Average in a statistical sense. For example in B1 enter:

=RollDice($A$1)

then copy B2 from B3 thru B1000

and finally use:

=MAX(B1:B1000)
=MIN(B1:B1000)
=AVERAGE(B1:B1000)
查看更多
甜甜的少女心
3楼-- · 2019-05-31 07:44

I would look at the functions RAND and RANDBETWEEN

Setup a table listing the dice choices 1 roll , 2 rolls etc and use those functions to calculate values from the inputs.

Keep the inputs separate from the table of calculations. Separate input from calculations and Separate output. You can even use different sheets in a workbook.

Don't worry about the notation at first, get the functions working.

Then you can format the output using string CONCATENATE to build up your notation.

Conditional formatting is only used once you have differing values in the excel cells and is probably not applicable here.

查看更多
登录 后发表回答