• ### What is your GameDev Story?

#### Archived

This topic is now archived and is closed to further replies.

# A little code problem I can't figure out.

This topic is 6885 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello everyone, I''m in the process of creating a character generator for an AD&D style game. For the character''s ability rolls, I''m using the code listed below. An example of the problem I''m having is that if you called MultiDiceRoll(3, 6) you would normally recieve scores between 3 and 18. For some reason the maximum score I get seems to be 12. The code is in Visual Basic. Think any of you could shed some light on what''s going on? I''m pulling my hair out over it, and can''t seem to figure it out! -- Begin Code ---------------------------------------------- Public Function DiceRoll(NumberOfSides As Integer) '' Rolls a dice with any number of sides. '' Declare a variable to hold the results of the dice roll. Dim RESULTS As Integer '' Generate a random number between 1 and NumberOfSides. Randomize RESULTS = Int((NumberOfSides * Rnd) + 1) '' Return results. DiceRoll = RESULTS End Function ------------------------------------------------------------ Public Function MultiDiceRoll(NumberOfDice As Integer, NumberOfSides As Integer) '' Rolls any number of dice chosen. Uses DiceRoll(). '' Declare an array to hold the dice roll results. Dim RESULTS(100) As Integer '' Declare a variable to keep count of the number of dice '' that have been rolled. Dim DICECOUNT As Integer '' Roll the required number of dice and store the results '' in RESULTS. DICECOUNT = 0 For DICECOUNT = 0 To NumberOfDice RESULTS(DICECOUNT) = DiceRoll(NumberOfSides) DICECOUNT = DICECOUNT + 1 Next '' Reset the counter. DICECOUNT = 0 '' Declare a variable to hold the dice total. Dim TOTAL As Integer '' Total the results of each dice roll. For DICECOUNT = 0 To NumberOfDice TOTAL = TOTAL + RESULTS(DICECOUNT) DICECOUNT = DICECOUNT + 1 Next '' Return TOTAL. MultiDiceRoll = TOTAL End Function -- End Code ------------------------------------------------ Thanks for any help, Reaptide

##### Share on other sites
Your for loop will automatically increment your loop counter, don''t increment it yourself inside the for loop.
That is: get rid of the DICECOUNT = DICECOUNT + 1 line inside the for loop.

##### Share on other sites
Oh crap, I can''t believe I did that.

Guess I was thinking about a C While Loop.

Thank you very much for your help, it is much appriciated.

##### Share on other sites

SiCrane spotted the real problem though.

Edited by - Spellbound on 3/13/00 12:39:45 PM

##### Share on other sites
Here you go:

function dice(a, b, c)
'' this returns adb+c (e.g. 1d4+1 for a magic missile
'' or 3d6+0 for a DEX score

total = 0
for x = 1 to a
total = total + Int((b * Rnd) + 1)
next

dice = total + c
end function

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634090
• Total Posts
3015432
×