hello everyone, I'm trying to generate numbers that are not repeated within 4 maps, but the algorithm is not so efficient, despite "it makes sense in my logic", can someone verify if there is no inconsistency with the "continue" inside the Do while ?
algorithm >
B4X:
Sub Process_Globals
Private Bot1_Cards As Map
End Sub
Sub AppStart (Args() As String)
Bot1_Cards.Initialize
For i = 0 To 15
Bot1_Cards.Put(i,GenerateDeckCardsUnique)
Next
Log("------------------------------------")
For i = 0 To Bot1_Cards.Size-1
Log("1x"&i&"-"&Bot1_Cards.Get(i))
Next
end sub
Sub GenerateDeckCardsUnique As Int
Dim isLoop As Int = 1
Dim GeneratedCard As Int
Do While ( isLoop = 1)
GeneratedCard = Rnd(0,51)
Log("GENERATED NUMBER = "&GeneratedCard)
If (Bot1_Cards.Size>0) Then
For j = 0 To Bot1_Cards.Size-1
Log("Card1="&Bot1_Cards.Get(j))
If (GeneratedCard = Bot1_Cards.Get(j)) Then
Log("BOT1 - CARD EXISTS")
Continue '
End If
Next
Else
Log("EMPTY FOR WHILE")
End If
isLoop = 0
Return GeneratedCard
Loop
End Sub
I'm not certain what you want your end-goal is, could it be something like below?
You have a deck of 52 cards (numbered 0-51), and you have four people. Each person should have unique 16 cards on hand in the end.
(I'm ignoring that 16x4 > 52, I assume you're only doing some testing.)
If this is correct, this is what you should do, written as skeleton code:
B4X:
Sub Process_Globals
Private Deck as List
End Sub
Sub AppStart (Args() As String)
FillDeck(52)
ShuffleDeck(1000)
Log(GetCardsFromDeck(16)) ' Person 1
Log(GetCardsFromDeck(16)) ' Person 2
Log(GetCardsFromDeck(16)) ' Person 3
Log(GetCardsFromDeck(16)) ' Person 4
end sub
Sub FillDeck(size as Int)
' Fill Deck with values that make sense to you
End Sub
Sub ShuffleDeck(iterations as Int)
' Loop for number of iterations: (which will decide how random it will be shuffled)
' Take card at random position and remove it from Deck, then place it at first position (or last, if that's easier)
End Sub
Sub GetCardsFromDeck(number as Int)
' Return number of cards from Deck (and also remove them from Deck when doing so)
End Sub
The continue keyword stops the current iteration (being a For, a While, ...) and starts the next. (as long a j < Bot1_Cards.Size). So, in your case the continue has effect on the For loop, not on the Do While. If you want it to jump out of the For loop, you can use Exit.