ilan Expert Licensed User Longtime User Jun 26, 2015 #1 can i get a random number except of a specific number? like dim rnd1 as int = rnd(1,10) but nr 2 & 5 not allowed.. is that possible?
can i get a random number except of a specific number? like dim rnd1 as int = rnd(1,10) but nr 2 & 5 not allowed.. is that possible?
Gunther Active Member Licensed User Longtime User Jun 26, 2015 #2 yes, not directly as you suggest, but do it with a loop: Code: Copy Dim intNewNumber As Int = Rnd(1, 10) ' Do Until (intNewNumber <> 2 or intNewNumber <> 5) intNewNumber = Rnd(1, 10) Loop ' If the Number is 2 or 5 a new RND is taken. Upvote 0
yes, not directly as you suggest, but do it with a loop: Code: Copy Dim intNewNumber As Int = Rnd(1, 10) ' Do Until (intNewNumber <> 2 or intNewNumber <> 5) intNewNumber = Rnd(1, 10) Loop ' If the Number is 2 or 5 a new RND is taken.
S sorex Expert Licensed User Longtime User Jun 26, 2015 #3 something like this? Code: Copy dim r as int r=2 ' might be needed to bypass an extra rnd call do while r=2 or r=5 r = rnd(1,10) loop Upvote 0
something like this? Code: Copy dim r as int r=2 ' might be needed to bypass an extra rnd call do while r=2 or r=5 r = rnd(1,10) loop
ilan Expert Licensed User Longtime User Jun 26, 2015 #4 thnax guys, can it be done without any loops? Upvote 0
ilan Expert Licensed User Longtime User Jun 26, 2015 #5 i made allready that kind of function but it makes my game flicker a little Code: Copy 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 Upvote 0
i made allready that kind of function but it makes my game flicker a little Code: Copy 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
wonder Expert Licensed User Longtime User Jun 26, 2015 #6 I have a similar issue, post your creative solutions, people! Upvote 0
S sorex Expert Licensed User Longtime User Jun 26, 2015 #7 I wonder why it should flicker if it only needs 1 iteration (best case)? Upvote 0
JordiCP Expert Licensed User Longtime User Jun 26, 2015 #8 Do the random for maxint minus one, and add one if the result is equal or more than forbiddennr Code: Copy 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 Upvote 0
Do the random for maxint minus one, and add one if the result is equal or more than forbiddennr Code: Copy 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
S sorex Expert Licensed User Longtime User Jun 26, 2015 #9 if the range is big enough you could risk Code: Copy dim r as int r=rnd(1,10) if r=2 or r=5 then r=rnd(1,10) which should use less resources Upvote 0
if the range is big enough you could risk Code: Copy dim r as int r=rnd(1,10) if r=2 or r=5 then r=rnd(1,10) which should use less resources
ilan Expert Licensed User Longtime User Jun 26, 2015 #11 JordiCP said: Do the random for maxint minus one, and add one if the result is equal or more than forbiddennr Code: Copy 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 Click to expand... what if the last number was = maxint then it can be that the same level will be loaded again Upvote 0
JordiCP said: Do the random for maxint minus one, and add one if the result is equal or more than forbiddennr Code: Copy 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 Click to expand... what if the last number was = maxint then it can be that the same level will be loaded again
ilan Expert Licensed User Longtime User Jun 26, 2015 #12 sorex said: is the range static? Click to expand... i have for example 10 levels and i want load them randomly but i dont want a level appear twice so i want to avoid the same level will be loaded again i tried also Code: Copy 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 Upvote 0
sorex said: is the range static? Click to expand... i have for example 10 levels and i want load them randomly but i dont want a level appear twice so i want to avoid the same level will be loaded again i tried also Code: Copy 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
ilan Expert Licensed User Longtime User Jun 26, 2015 #13 sorex said: if the range is big enough you could risk Code: Copy dim r as int r=rnd(1,10) if r=2 or r=5 then r=rnd(1,10) which should use less resources Click to expand... to risky Upvote 0
sorex said: if the range is big enough you could risk Code: Copy dim r as int r=rnd(1,10) if r=2 or r=5 then r=rnd(1,10) which should use less resources Click to expand... to risky
Gunther Active Member Licensed User Longtime User Jun 26, 2015 #14 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! Code: Copy dim r as int r=rnd(1,10) if r=2 or r=5 then r = r + 1 This is faster but not that flexible as my first one. Upvote 0
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! Code: Copy dim r as int r=rnd(1,10) if r=2 or r=5 then r = r + 1 This is faster but not that flexible as my first one.
Gunther Active Member Licensed User Longtime User Jun 26, 2015 #15 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 Upvote 0
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
ilan Expert Licensed User Longtime User Jun 26, 2015 #16 Gunther said: 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! Code: Copy dim r as int r=rnd(1,10) if r=2 or r=5 then r = r + 1 This is faster but not that flexible as my first one. Click to expand... this will not work Upvote 0
Gunther said: 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! Code: Copy dim r as int r=rnd(1,10) if r=2 or r=5 then r = r + 1 This is faster but not that flexible as my first one. Click to expand... this will not work
T Troberg Well-Known Member Licensed User Longtime User Jun 26, 2015 #17 Put the allowed numbers in an array, then simply pick an item from the array at random. Slower if you only need to do it occasionally, but if you need to do it often and can prepare the array in advance, it will be fast, and it will have a predictable running time, which the loop methods won't. Upvote 0
Put the allowed numbers in an array, then simply pick an item from the array at random. Slower if you only need to do it occasionally, but if you need to do it often and can prepare the array in advance, it will be fast, and it will have a predictable running time, which the loop methods won't.
S sorex Expert Licensed User Longtime User Jun 26, 2015 #18 shuffle a level array at each game start Code: Copy 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)) chances that the levels will be the same in a row az the previous game are very unlikely Upvote 0
shuffle a level array at each game start Code: Copy 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)) chances that the levels will be the same in a row az the previous game are very unlikely
ilan Expert Licensed User Longtime User Jun 26, 2015 #19 Gunther said: 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 Click to expand... i want the levels load randomly again and agian but not same level twice after he was loaded. but he can be again loaded if another level was before like 2,4,5,3,2,4,2,6,7,5,6,7,.... Upvote 0
Gunther said: 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 Click to expand... i want the levels load randomly again and agian but not same level twice after he was loaded. but he can be again loaded if another level was before like 2,4,5,3,2,4,2,6,7,5,6,7,....
T Troberg Well-Known Member Licensed User Longtime User Jun 26, 2015 #20 Gunther said: 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! Code: Copy dim r as int r=rnd(1,10) if r=2 or r=5 then r = r + 1 This is faster but not that flexible as my first one. Click to expand... That would give skewed probabilities, where 3 and 6 are twice as likely to appear as the other numbers. Upvote 0
Gunther said: 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! Code: Copy dim r as int r=rnd(1,10) if r=2 or r=5 then r = r + 1 This is faster but not that flexible as my first one. Click to expand... That would give skewed probabilities, where 3 and 6 are twice as likely to appear as the other numbers.