Can you pull a random number then remove it from the list?

PharCyDeD

Active Member
Licensed User
Longtime User
I know I can get a random number, by simply do this:

B4X:
RandomNumber = Rnd(1, 10)

However, is there any way to record the number and then remove that number from being randomly chosen again?

For instance, if I ran that through a loop or something I would eventually have all the numbers possible but no duplicates?
 

PharCyDeD

Active Member
Licensed User
Longtime User
Yes, that sounds similar to what I am needing...well basically a lot like what I need really. I was going to use the random number to be assigned to ImageViews then move the ImageViews around in a random order (to specific locations but random in choosing the location) on the 'Activity' to basically randomize the screen. I am not using cards, but basically the same concept. Can you provide some help on how you accomplished it?
 
Last edited:
Upvote 0

PharCyDeD

Active Member
Licensed User
Longtime User
I was looking over VB code and came across this:

B4X:
Dim X As Long
Dim r1 As Long
Dim Temp As String
Randomize
For X = 0 To (List1.ListCount - 1)
r1 = (List1.ListCount - 1) * Rnd
Temp = List1.List(X)
List1.List(X) = List1.List(r1)
List1.List(r1) = Temp

I am unsure of how to apply it to B4A though since there is no ListCount for ListView.

:sign0163:
 
Upvote 0

LineCutter

Active Member
Licensed User
Longtime User
http://www.b4x.com/forum/share-your-creations/620-shuffling-cards-proof-concept.html
(The search function for the forums is up the top, on the right)
Here is the code:
B4X:
Sub Globals
dim cards(52)
End Sub

Sub App_Start
 setuporder
 order
 filllist
 Form1.show
 num1.minimum=-50
 num1.maximum=50
End Sub

Sub Button1_Click
 shuffle
 filllist
End Sub

sub filllist
 listbox1.clear
 for i = 0 to arraylist1.count-1
  listbox1.add(arraylist1.item(i))
 next
 listbox1.refresh
end sub

sub order
 arraylist1.clear
 for I= 0 to 51
  arraylist1.add(cards(i))
 next
end sub

sub copyarraylist
 arraylist2.clear
 for I=0 to arraylist1.count-1
   arraylist2.add(arraylist1.item(i))
 next
 arraylist1.clear
end sub

sub shuffle
 copyarraylist
 do while arraylist2.count >0
  I=rnd(0,arraylist2.count-1)
  arraylist1.add(arraylist2.item(i))
  arraylist2.removeat(i)
 loop
end sub

Sub setuporder
 arraylist1.clear
 s="HCDS"
 r=0
 suit=0
 for I=0 to 51
  r=r+1
 cards(i)=substring("0"&r,strlength("0"&r)-2,2)&substring(s,suit,1)
  if r=13 then
   R=0
   suit=suit+1
  end if
 next
end sub

Sub Button2_Click
 order
 filllist
End Sub

Sub Button3_Click
arraylist1.sort(cCaseSensitive)
filllist
End Sub

Sub FilterButton_Click
filterout
filllist
end sub

sub filterout
if num1.value <>0 then
copyarraylist
arraylist2.sort(cCaseSensitive)
t=round(arraylist2.count/2)
b=0
if num1.value >0 then b=t
iterations=round(t-(abs(num1.value)*2*t/100))
'msgbox(t&" "&b&" "&iterations&" "&(b+t-1))
for i = (b+t-1) to b step -1
arraylist1.add(arraylist2.item(i))
arraylist2.remove(arraylist2.item(i))
next
do while iterations >0
p=rnd(0,arraylist2.count-1)
arraylist1.add(arraylist2.item(p))
arraylist2.removeat(p)
iterations = iterations -1
loop
end if
End Sub
 
Upvote 0

PharCyDeD

Active Member
Licensed User
Longtime User
I do appreciate your help but it is hard to search for something you don't know exists . You stated you did a routine that handled it...not that you shared it on the forums. It looks very similar to some code I looked at earlier for VB 6.0. I tried this:

B4X:
Dim Num(1 To 6) As Int
   Dim Num As Int
   Dim Low As Int
   Dim High As Int
   Dim x As Int
   Dim y As Int
   Dim Result As String

        High = 40
   Low = 1

   'Generate lotto numbers
   For x = 1 To 6
   Num(x) = ((High - Low) * Rnd) + Low
   'Detect duplicates
   For y = 1 To x
   If x <> y Then
   If Num(x) = Num(y) Then x = x - 1
   Else
   End If
   Next y
   Next x
   
   Result = " " & Num(1) & " " & Num(2) & " " & Num(3) & " " & Num(4) & " " & Num(5) & " " & Num(6)
   ListView1.AddSingleLine(Result)

but I get this:

Compiling code. Error
Error parsing program.
Error description: Use of undeclared array: num
Occurred on line: 1247
Num(x) = ((High - Low) * Rnd) + Low
 
Upvote 0

PharCyDeD

Active Member
Licensed User
Longtime User
i fixed it by doing:

B4X:
    Dim Num(6) As Int

But now getting:

Compiling code. Error
Error compiling program.
Error description: Missing parameter.
Occurred on line: 1243
Num(x) = ((High - Low) * Rnd) + Low
Word: )
 
Upvote 0

JonPM

Well-Known Member
Licensed User
Longtime User
For Rnd you didn't specify the min and max, it needs to be something like Rnd(1,6) I believe

Sent from my DROIDX
 
Upvote 0

PharCyDeD

Active Member
Licensed User
Longtime User
Jon you are right about Rnd thanks! I changed my code to:

B4X:
Dim Num(6) As Int
    Dim Num As Int
    Dim Low As Int
    Dim High As Int
    Dim x As Int
    Dim y As Int
    Dim Result As String

    High = 40
    Low = 1

    'Generate lotto numbers
    For x = 1 To 6
    Num(x) = ((High - Low) * Rnd(1,37)) + Low
    'Detect duplicates
    For y = 1 To x
    If x <> y Then
    If Num(x) = Num(y) Then x = x - 1
    Else
    End If
    Next y
    Next x
    
    Result = " " & Num(1) & " " & Num(2) & " " & Num(3) & " " & Num(4) & " " & Num(5) & " " & Num(6)
    ListView1.AddSingleLine(Result)

It is now telling me:

Compiling code. Error
Error parsing program.
Error description: Use of undeclared array: num
Occurred on line: 8616
Num(x) = ((High - Low) * Rnd(1,37)) + Low

I don't understand why?
 
Upvote 0

JonPM

Well-Known Member
Licensed User
Longtime User
Because you are declaring num twice:
Dim Num(6) As Int
Dim Num As Int

Delete the second one
 
Upvote 0

LineCutter

Active Member
Licensed User
Longtime User
I do appreciate your help but it is hard to search for something you don't know exists . You stated you did a routine that handled it...not that you shared it on the forums.
Fair point. I'd assumed that the implication was obvious. I hope that the code was some use.
 
Upvote 0

JohnD

Active Member
Licensed User
Longtime User
This is how I got 10 non repeating values for the numbers 0 through 9 inclusive.
B4X:
Sub RandomNumbers
 
Dim Num(10) As Int
Dim x As Int
Dim y As Int
Dim Result As String   
Dim bFound As Boolean
 
For x = 0 To 9
       
    Num(x) = Rnd(0,10)                       
    bFound = True
 
    Do Until bFound = False
       
        For y = 0 To x
            bFound = False
            If x <> y Then
                If Num(x) = Num(y) Then           
                    Num(x) = Num(x) - 1               
                    If Num(x) < 0 Then
                        Num(x) = Num(x) + 10
                    End If
                    bFound = True
                    Exit
                End If
            End If
        Next
       
    Loop
   
Next
 
Result = " " & Num(0) & " " & Num(1) & " " & Num(2) & " " & Num(3) & " " & Num(4) & " " & Num(5) & " " & Num(6) & " " & Num(7) & " " & Num(8) & " " & Num(9)
Msgbox(Result, "Result")
   
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…