I want to create a random number, but the number should be used only one time so that a number don't turn up twice.
I made a working sample with 6 random numbers.
In code row nr. 63 I check if the number I already used turn up again:
(Attached is the sample program)
Is there a smater (and shorter) way to check for used numbers? Because in my app I need to check 44 numbers.
All the code:
I made a working sample with 6 random numbers.
In code row nr. 63 I check if the number I already used turn up again:
B4X:
If rndNr = usedNr(1) OR rndNr = usedNr(2) OR rndNr = usedNr(3) OR rndNr = usedNr(4) OR rndNr = usedNr(5) OR rndNr = usedNr(6) Then
(Attached is the sample program)
Is there a smater (and shorter) way to check for used numbers? Because in my app I need to check 44 numbers.
All the code:
B4X:
Sub Globals
Private btnRndNr As Button
Private labRndNr As Label
Dim rndNr As Int
Dim usedNr(7) As Int
Dim count As Int
count = 1
Private labCount As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Main")
End Sub
Sub btnRndNr_Click
rndNr = Rnd(1, 7) ' Create random nr. 1 to 6
' If random nr. was used already, start again
If rndNr = usedNr(1) OR rndNr = usedNr(2) OR rndNr = usedNr(3) OR rndNr = usedNr(4) OR rndNr = usedNr(5) OR rndNr = usedNr(6) Then
btnRndNr_Click
Else ' Else display random nr.
labRndNr.Text = "Rnd Nr. = " & rndNr
usedNr(count) = rndNr 'usedNr get value
'counter
labCount.Text = "Count = " & count
count = count +1
If count = 7 Then 'If counter is 7, reset to 1 and set usedNr to 0
count = 1
For i = 0 To 6
usedNr(i) = 0
Next
End If
End If
End Sub