Måste man inte ge Seed ett värde?
Frågan#1
Halloj!
Håller i min VB-kurs på Malmöhögskola på att programmera en tärningsfunktion.
De hela bygger egentligen på två tärningar vars värden slumpas fram med hjälp av Rnd().
Användaren av programmet ska också kunna ange hur många gånger de två tärningarna ska kastas och därefter få ett svar på hur många gånger värdena på de två tärningarna blev likadana och hur många gånger de fått dubbel sexor.
Mitt program genererar dessvärre 0 i de båda resultat rutorna hela tiden :(
Så här ser min kod ut:
Option Explicit
' Read indata
Function readData(ByVal intAmount As Integer) As Boolean
Dim txtRawValue As String
Dim blnOkey As Boolean
txtRawValue = txtDiceAmount.Text
If IsNumeric(txtRawValue) Then
intAmount = Int(txtRawValue)
blnOkey = True
Else
MsgBox "Du måste ange ett tal i siffror!"
txtDiceAmount.SetFocus
txtDiceAmount.SelStart = 0
txtDiceAmount.SelLength = Len(txtDiceAmount.Text)
blnOkey = False
End If
readData = blnOkey
End Function
' Hit the dice!
Function hitDice(ByVal rangeStart As Integer, ByVal rangeEnd As Integer) As Integer
Dim rndAmount As Integer
rndAmount = Int(Rnd * (rangeStart - rangeEnd + 1) + rangeEnd)
hitDice = rndAmount
End Function
' Get dice results
Function getResults(ByVal int6 As Integer, ByVal intSame As Integer, ByRef intAmount As Integer) As Boolean
Dim rndNumber1 As Integer
Dim rndNumber2 As Integer
Dim i As Integer
Dim tmp6 As Integer
Dim tmpSame As Integer
i = 1
tmp6 = 0
tmpSame = 0
Do While i <= intAmount
rndNumber1 = hitDice(1, 6)
rndNumber2 = hitDice(1, 6)
' Check Amounts
If rndNumber1 = rndNumber2 Then
' Same amount
If tmpSame = 0 Then
tmpSame = 1
Else
tmpSame = tmpSame + 1
End If
' Both 6?
If rndNumber1 = 6 And rndNumber2 = 6 Then
' Both 6, count
If tmp6 = 0 Then
tmp6 = 1
Else
tmp6 = tmp6 + 1
End If
End If
End If
i = i + 1
Loop
int6 = tmp6
intSame = tmpSame
getResults = True
End Function
Sub displayResult(ByRef intB6 As Integer, ByRef intS As Integer)
lblResultSame.Caption = CStr(intS)
lblResult6.Caption = CStr(intB6)
' Reset amount field
txtDiceAmount.Text = ""
End Sub
Private Sub cmdDice_Click()
Dim intDiceAmount As Integer
Dim intBoth6 As Integer
Dim intBothSame As Integer
' Read indata
If readData(intDiceAmount) Then
' Get result data
If getResults(intBoth6, intBothSame, intDiceAmount) Then
' Display results
Call displayResult(intBoth6, intBothSame)
End If
End If
End Sub
Private Sub Form_Load()
Randomize
End Sub
