yesterday I tried to implement a dice-roller in basic and use it as a macro. That worked. Then I wanted to see how "random" the rnd() function actually is. To do that I created a function that generates a number of rolls (with a fixed die-size), writes the result into an array. Since I use a 20-sided die, I assumed that an array with 20 entries would suffice. Yet I was mistaken as I encountered out of bounds accesses. Looking deeper into this I found that my function would produce values in the range of (1:21) which was clearly not intended. However, I fail to see what this is caused by.
Code: Select all
function fnf()
Randomize
Dim value as Integer
Dim values(19) as Long
For I = 1 to 100000
value = CInt(Int(rnd()* 20)+1)
values(value-1) = values(value-1) + 1
Next I
rstring = ""
For I=0 to 19
rstring = rstring & CStr(I+1) & ":" & CStr(values(I)) & ", "
Next I
MsgBox rstring
End function