Android Question random number except of one number...

ilan

Expert
Licensed User
Longtime User
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?
 

JordiCP

Expert
Licensed User
Longtime User
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,....

That's what I understand ilan is asking for
 
Upvote 0

Gunther

Active Member
Licensed User
Longtime User
ok, that is a little different than since in #1 you asked to avoid 2 numbers...

B4X:
Dim intNewNumber As Int = Rnd(1, 10), intOldNumber as Int = intNewNumber
'
Do Until intNewNumber <> intOldNumber

   intNewNumber = Rnd(1, 10)

Loop
intOldNumber = intNewNumber
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Why does my answer not work?

hi jordiCp@

your code:

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

the problem is if the forbidennumber was 10 (last level) and rnd1 was again 10 then the app will look for a new level that does not exist (level 11)

actually you are correct, it may work
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
simular as doing it without a list but with just an array, it limits flexibility unless you clear and refill the list again.
 
Upvote 0

Troberg

Well-Known Member
Licensed User
Longtime User
Even neater solution:

Put all the levels in a list in a random order. Then, take a random one from the first 8 (assuming 10 levels and 2 barred). Then, simply put the used one at the end of the list. The next time, just re-use the list.
 
Upvote 0

Troberg

Well-Known Member
Licensed User
Longtime User
Arrays can be de
Whatever you can do with arrays, you can do it with Lists or Maps (in B4X) but not vice versa.

Arrays can be declared as being of a type. You can't do that with a map or list (something which would be very nice to be able to do).
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…