sub selectlevel
Dim lv As Int = randomfunction(maxlevels,pnl1.Tag)
end sub
'....
Sub randomfunction(maxint As Int,forbiddennr As Int) As Int
Dim rnd1 As Int = forbiddennr
Do Until rnd1 <> forbiddennr
rnd1 = Rnd(2,maxint)
Loop
Return rnd1
End Sub
what if the last number was = maxint then it can be that the same level will be loaded againDo the random for maxint minus one, and add one if the result is equal or more than forbiddennr
B4X:Sub randomfunction(maxint As Int,forbiddennr As Int) As Int Dim rnd1=Rnd(1,maxint-1) if rnd1>=forbiddennr then rnd1=rnd1+1 end if End Sub
i have for example 10 levelsis the range static?
Dim lv As Int = Rnd(2,maxlevels+1) 'choose random level except of level1 and same level
If lv <> pnl1.Tag Then
pnl2.LoadLayout("level" & (lv))
pnl2.Tag = lv
Else
pnl2.LoadLayout("level" & Min((pnl1.Tag+1),maxlevels))
pnl2.Tag = Min((pnl1.Tag+1),maxlevels)
End If
to riskyif the range is big enough you could risk
which should use less resourcesB4X:dim r as int r=rnd(1,10) if r=2 or r=5 then r=rnd(1,10)
this will not workWell, here is an other one!
But the forbidden numbers need not be in a row, e.g. 2,3 or 6,7 and never 9!
This is faster but not that flexible as my first one.B4X:dim r as int r=rnd(1,10) if r=2 or r=5 then r = r + 1
Dim r As Int
Dim t As Int
Dim levels() As Int=Array As Int(1,2,3,4,5,6,7,8,9,10)
For x=0 To levels.Length-1
r=Rnd(0,levels.Length)
t=levels(r)
levels(r)=levels(x)
levels(x)=t
Next
Log(levels(0))
i want the levels load randomly again and agian but not same level twice after he was loaded.what you mean by ?
"...load them randomly but i dont want a level appear twice..."
1) all 10 levels shall be used once and than never again?
2) or not directly after a level is done?
for 1) you may shuffle the number in the beginnig and doing a list from 1 to 10
for 2) see #14 or #2
That would give skewed probabilities, where 3 and 6 are twice as likely to appear as the other numbers.Well, here is an other one!
But the forbidden numbers need not be in a row, e.g. 2,3 or 6,7 and never 9!
This is faster but not that flexible as my first one.B4X:dim r as int r=rnd(1,10) if r=2 or r=5 then r = r + 1