Android Question Random file 2 Listviews

daniedb

Active Member
Licensed User
Longtime User
Hi Guys

I try to random fill 2 Listviews with numbers 1-10
Both Lists must have number 1-10, in a Random format for each listview
I keep missing numbers, even when looping a 1000 times

Anysuggestion?
Here is my current code
B4X:
Sub StartBtn_Click
   l1.Initialize
   l2.Initialize
   l1.Clear
   l2.Clear
   Dim i As Int
   For i = 0 To 1000
     fillnumber(Rnd(1,11),l1)
   Next

   For i = 0 To 1000
     fillnumber(Rnd(1,11),l2)
   Next
End Sub

Sub fillnumber(num As Int, fl As List)
   Dim n As Int
   Dim ex As String
   ex="N"
   If fl.Size = 0 Then
     fl.Add(num)  
   End If
   If fl.Size < 11 Then
     For n=0 To fl.Size-1
       fl.Get(n)
       If fl.Get(n) = num Then
         ex="Y"    
       End If  
     Next
     If ex="N" Then
       fl.Add(num)
       Log(num)
     End If
   End If
End Sub

Thanks
Danie
 

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
Are you looking to randomize the order of a list of items? If so, then use a shuffle algorithm such as:
B4X:
Sub Process_Globals
    Dim foList As List
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim poButton As Button
    poButton.Initialize("btnEnd")
    Activity.AddView(poButton, 10dip, 10dip, 100dip, 50dip)
    poButton.Text = "Exit"
End Sub

Sub Activity_Resume
    BuildList
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Private Sub btnEnd_Click
    Activity.Finish
End Sub

Private Sub BuildList
    Dim piIndex As Int
  
    foList.Initialize
    For piIndex = 1 To 10
        foList.Add(piIndex)
    Next
    RandomizeListOrder
  
    For piIndex = 0 To foList.Size - 1
        Log("Entry #" & piIndex & " contains: " & foList.Get(piIndex))
    Next
End Sub

Private Sub RandomizeListOrder
    Dim piIndex As Int
    Dim piTar As Int
  
    For piIndex = foList.Size - 1 To 0 Step - 1
        piTar = Rnd(0, piIndex + 1)
        Dim piInt As Int
        piInt = foList.Get(piTar)
        foList.Set(piTar, foList.Get(piIndex))
        foList.Set(piIndex, piInt)
    Next
End Sub
 
Last edited:
Upvote 0

eurojam

Well-Known Member
Licensed User
Longtime User
Try for example (not tested, just an idea)
B4X:
Sub fill_random_list As List
    Dim m As Map
    Dim v As Int
    m.Initialize
    Do While m.Size < 10
      v = Rnd(1,11)
      m.put(v,v)
    Loop
    Return m.Keys
   
End Sub
 
Upvote 0

eurojam

Well-Known Member
Licensed User
Longtime User
and can take a while until is filled, may be the sort idea is the better one
 
Upvote 0

daniedb

Active Member
Licensed User
Longtime User
Hi Guys

Thanks for all the ideas.
Come accross this code in the forum
B4X:
Sub RndUniqueNums(MinValue As Int, MaxValue As Int, NumOfValues As Int) As List
  Private lstAvailable As List
  lstAvailable.Initialize
   Dim N As Int
  For N = MinValue To MaxValue
  lstAvailable.Add(N)
  Next
  Private Available As Int
  Available = MaxValue - MinValue + 1
  Private RetList As List
  RetList.Initialize
  Private NumDispIdx As Int
  For i=0 To NumOfValues-1
  NumDispIdx = Rnd(1, Available+1)-1
  RetList.Add(lstAvailable.Get(NumDispIdx))
  lstAvailable.RemoveAt(NumDispIdx)
  Available = Available -1
  Next
  Return RetList
End Sub

Sofar tested and it looks like it will do the job

Appreciate
Danie
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…