r/visualbasic • • Mar 27 '20

Visual Basic Dungeons and Dragons Dice Roller Help

So for my Visual Basic Programming class, we have to make our own project. I am trying to make a dungeons and dragons dice roller, but I am having trouble getting the Rnd function to roll dice depending on the amount of dice the user wants to roll.

For Example: If the user wants to roll five 6 sided die, the program should roll the D6 five times and add them all together. Then display the added number.

If you would like to message me and ask me further questions, feel free! Thanks for the help in advance!

/preview/pre/zsazyk6nk9p41.png?width=445&format=png&auto=webp&s=2ea75889e265ee419af6b413aa79a702dfe3b6f8

/preview/pre/eutrigflk9p41.png?width=1805&format=png&auto=webp&s=4b43de26da203378399544a66674a96fbe67911e

6 Upvotes

8 comments sorted by

2

u/ClootBooters24 Mar 27 '20

Yup, I figured it out thanks to a user on here! Thanks for all the quick responses and dealing with a beginner!

2

u/Thunor_SixHammers Mar 27 '20

This way may not be the most efficient, but its what I tend to do

Dim chance as random = new random

dice1 = chance.next(1,5)

You need to define what number range the dice will pick from. Think about it, if I asked you "Think of a random number" you have between infinity and infinity to pick from. But if I tell you "Pick between 1 and 5" you have 4 numbers to choose from (the way random works, unless I have it backwards, the minimum number is inclusive, and the max is exclusive, so 1-5 will return 1-4; no need to add any numbers at the end)

Edit: Spelling and I forgot some important bits

1

u/ClootBooters24 Mar 27 '20

Let me try that real fast!

2

u/banshoo Mar 27 '20

Also, (if I'm reading this rightly) :

Your letting the person pick 4 * 6 sided dice But your code is adding together the result of a random result & multiplying it by 4

so that means that instead of an actual potential of : 4 - 24, Your results can only be : 4, 8, 12, 16, 20, 24 & then adding 1, for some reason

Use a For (or another method) loop for the count of die needed, and add to a running total.

1

u/ocdtrekkie Mar 27 '20

It's a bit of an odd choice that you defined all the variables as Doubles if you're putting Ints in them anyways. Is there any reason not to just use Integer for defining the variables as well?

I have to be honest, I have no idea what you're doing with SelectedIndex here. I think your mistake may sit there more than anything else. Your random function seems fine, but you're not telling it how many times to roll each dice.

1

u/Thunor_SixHammers Mar 27 '20

My guess is that this code is still in the works, and the selectindex is going to be used to determine the math for the dice roll. This is far from the most optimal way to do it, but an understandable attempt from a beginner;

Ie, if Select Index = 1, roll 1d5, if select index = 2, roll 2d5

1

u/PostalElf VB.Net Intermediate Mar 27 '20

Here's how I would do it. I would add in each combobox's Tag property the value of the die (4 for the d4, 6 for the d6 etc). I would then add all of the comboboxes into a list at Form.Load - let's call this DiceList - and when the button is clicked, I'll run the following code:

Dim rng as New Random
Dim total as Integer = 0
For each cmb as ComboBox in DiceList
  If cmb.SelectedIndex = -1 Then Continue For
  Dim dieValue as Integer = Convert.ToInt32(cmb.SelectedItem)
  total += rng.next(1, dieValue + 1)
Next
' now update the textbox with the total value stored in total

1

u/thudly Mar 27 '20

Here's some code I wrote for my own DnD-style adventure game. Hope this helps.

''' <summary>
''' Roll a number of dice of certain size, and add some modifier
''' </summary>
''' <param name="rolls">Number of dice to roll.</param>
''' <param name="diceSize">The size of the dice to roll. 4, 6, 8, 10, 12, 20, etc.</param>
''' <param name="modifier">The modifier to add to the final roll. +2, -1, etc.</param>
''' <returns>An integer with resulting roll, plus modifier.</returns>
Public Function Roll(rolls As Integer, diceSize As Integer, Optional modifier As Integer = 0) As Integer
    Dim r As Integer

    For i = 1 To rolls
        r += RndInt(1, diceSize)
    Next

    r += modifier

    Return r
End Function

''' <summary>
''' Returns a random integer between min and max, inclusive
''' </summary>
''' <param name="min">the minimum range</param>
''' <param name="max">the maximum range, inclusive</param>
''' <returns>An integer between min and max, inclusive</returns>
Public Function RndInt(min As Integer, max As Integer) As Integer
    Randomize()
    Return Math.Floor(min + (Rnd() * (max - min + 1)))
End Function