Android Question Combine two lists the shuffle

dibb3386

Member
Licensed User
Longtime User
Hi everyone, been stuck on this for a few days now and cant wrap my head around it.

I need to take a random amount of lines from each txt file and keep a count on how many have been picked. Done that but now i need to shuffle the two lists in to 1 list and then add to a list view.

Am able to shuffle each list seperately and add to listview however 1.txt list shows above 2.txt where i want them shuffled again together.

Any help with this would be great, am sure its simple as am adding to a list from text file and shuffling already but just cant seem to figure it out for both lists

Thanks


B4X:
Sub Process_Globals
    
    Dim playlist As List
    
End Sub
Sub Globals
    Private ListView1 As ListView
    
    Private NewList1 As List
    Private NewList2 As List
    
    Private RndNo1 As Int = Rnd(1,10)
    Private RndNo2 As Int = Rnd(1,10)
    
End Sub
Sub Activity_Create(FirstTime As Boolean)

    Activity.LoadLayout("main")
    

    playlist.Initialize
    
    

    
    Populate1
    
    
End Sub

Sub Populate1
    Dim lSize As Int = RndNo1
    
    Dim nrMin As Int = 1
    Dim nrMax As Int = 10
    Dim llist As List = GetRndNonRepeatingList(lSize, nrMin, nrMax)
    Dim AddLine As String
    Log(RndNo1&" Selected: ")
    Log(RndNo2&" Selected: ")
    NewList1 = File.ReadList(File.DirAssets,"1.txt")
    NewList2 = File.ReadList(File.DirAssets,"2.txt")
    
    For Each n As Int In llist
        'Log(AddLine)
        'Log(n)
    
        
        AddLine = NewList1.Get(n)
        
        NewList1 = ShuffleList(NewList1)
        ListView1.AddSingleLine(AddLine)
        
    Next

    Dim lSize2 As Int = RndNo2
    
    Dim nrMin2 As Int = 1
    Dim nrMax2 As Int = 10
    Dim llist2 As List = GetRndNonRepeatingList(lSize2, nrMin2, nrMax2)
    
    Dim AddLine2 As String
    
    For Each n As Int In llist2
        
        
        AddLine2 = NewList2.Get(n)
        
        NewList2 = ShuffleList(NewList2)
        ListView1.AddSingleLine(AddLine2)
    

    Next




End Sub

Sub ShuffleList(pl As List) As List
    For i = pl.Size - 1 To 0 Step -1
        Dim j As Int
        Dim k As Object
        j = Rnd(0, i + 1)
        k = pl.Get(j)
        pl.Set(j,pl.Get(i))
        pl.Set(i,k)
    Next
    Return pl
End Sub



Sub GetRndNonRepeatingList(lSize As Int, rnMin As Int, rnMax As Int) As List
    Dim llist As List
    Dim nr As Int
    llist.Initialize
    Private nNativeMe As JavaObject
    nNativeMe.InitializeContext
    If rnMax+1>=lSize And rnMin<rnMax Then
        Do While llist.Size< lSize
            nr=nNativeMe.RunMethod("GetRndJAVAInt", Array(rnMin,rnMax))
            If llist.IndexOf(nr)=-1 Then
            
                llist.Add(nr)
            End If
        Loop
    End If
    Return llist
    
End Sub
#If JAVA
import java.util.Random;
public static int GetRndJAVAInt(int min, int max) {
    Random random = new Random();
    return random.nextInt((max - min) + 1) + min;
}
#End If
 

emexes

Expert
Licensed User
I am not at my computer, but: if the problem is that you want the two lists intermingled randomly together, you could do something like:
B4X:
do while (list1.size + list2.size) > 0
    ItemNumber = rand(0, list1.size + list2.size)
    If ItemNumber < List1.Size then
        BigList.Add(List1.Get(ItemNumber)
        List1.Delete(ItemNumber)
    Else
        'Convert List1+2 index to List2 index
        ItemNumber = ItemNumber -         List1.Size
        BigList.Add(List2.Get(ItemNumber)
        List2.Delete(ItemNumber)
    End If
Loop
I might have some keywords wrong. I will tidy it up when I have a real browser to work with.
:)
 
Last edited:
Upvote 0

dibb3386

Member
Licensed User
Longtime User
This is the result i need, except I need to get how many items in the list were taken from each list which is based of rnd(1,10) for each list, i.e. 6 from 1.txt, 5 from 2.txt etc. so combining the list like this i can not get those numbers. :confused:
 
Upvote 0

dibb3386

Member
Licensed User
Longtime User
I am not at my computer, but: if the problem is that you want the two lists intermingled randomly together, you could do something like:
B4X:
do while (list1.size + list2.size) > 0
    ItemNumber = rand(0, list1.size + list2.size)
    If ItemNumber < List1.Size then
        BigList.Add(List1.Get(ItemNumber)
List1.Delete(ItemNumber)
Else
    'Convert List1+2 index to List2 index
    ItemNumber = ItemNumber - List1.Size
BigList.Add(List2.Get(ItemNumber)
List2.Delete(ItemNumber)
End If
Loop
I might have some keywords wrong, and the indentation looks fcuked on this phone. I will tidy it up when I have a real browser to work with.
:)
I was typing my response to Erel and didnt notice your reply, ill look into this now, thanks for helping :D
 
Upvote 0

emexes

Expert
Licensed User
You can shuffle a list in-place by:
B4X:
For Shuffle = 1 to List.Size
    Get a random item from the list
    Delete that item from list
    Add item back to list (at end)
Next
That might not be statistically rigorous, but should be good enough to shuffle songs.
 
Upvote 0
Top