Android Code Snippet Get Random Numbers in a non repeating list

Myr0n

Active Member
Licensed User
The next code snippet, return a list with non repeating random numbers
Feel free to modify or use it as you want.
This code depends on JavaObject and Inline Java Code usage.

I decide to use the inline java because the Core random function (RND) get stuck at some point, I do not have the Core random function code to compare with my code, but a similar situation happened when I took off the number 1 in my inline java code.
B4X:
random.nextInt((max - min) + 1) + min;


B4X:
'Return As List : A list with random numbers
'lSize As Int : Number of elements to return in the list
'rnNub As Int : Number minimum for the random range
'rnMax As Int : Number maximum for the random range
'Code Example:<code>
'    Dim listOfRndNumbers As List
'    listOfRndNumbers.initialize
'    'Next line return a List with 10 items with random numbers where the minimum is 20 and the maximum is 777
'    listOfRndNumbers=GetRndNonRepeatingList(10,20,777)
'    For Each n As Int In listOfRndNumbers
'        Log(n)
'    Next</code>
'This Sub depends on JavaObject
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

The next code is an addition of my original post as an example of how to use it
B4X:
    Dim lSize As Int = 3
    Dim nrMin As Int = 0
    Dim nrMax As Int = 2
    Dim llist As List = GetRndNonRepeatingList(lSize, nrMin, nrMax)
    llist.Sort(True)
    For Each n As Int In llist
        Log(n)
    Next
The output should be
B4X:
0
1
2
Enjoyed!!
 
Last edited:

Myr0n

Active Member
Licensed User
You can do this with a map, too as the same key (= random number) overrides the existing one. By the way never had issues with the rnd function.
Thank you for your feedback and good for you that you never had issues with the rnd function.

About what you said first, can you show us, sharing to us a complete example, that will have the same result as the code snippet that is in the post #1, please.

Thank you
 

mc73

Well-Known Member
Licensed User
Just a thought:
B4X:
Sub GetRndNonRepeatingList(lSize As Int, rnMin As Int, rnMax As Int) As List
   Private lstRandom As List
   lstRandom.Initialize
   If rnMax-rnMin>=lSize Then
       Private lstBase As List
       lstBase.Initialize
       For i=rnMin To rnMax-1
           lstBase.Add(i)
       Next
       For k=0 To lSize-1
           Private tempRandom As Int=Rnd(0,lstBase.Size)
           lstRandom.Add(lstBase.Get(tempRandom))
           lstBase.RemoveAt(tempRandom)
       Next
   End If
   Return lstRandom
End Sub
 

KMatle

Expert
Licensed User
About what you said first, can you show us, sharing to us a complete example
Here you go (unsorted & no check if e.g. MinNumber > MaxNumber or NumbersToCreate > MaxNumber)

B4X:
CreateNumbers(10,0,1000)
B4X:
Sub CreateNumbers (NumbersToCreate As Int, MinNumber As Int, MaxNumber As Int) As Map

    Dim NumbersMap As Map
    NumbersMap.Initialize
        
    Do Until NumbersMap.size=NumbersToCreate
       NumbersMap.Put(Rnd(MinNumber,MaxNumber+1),Null)     
    Loop
    
    Return (NumbersMap)
End Sub
 

Mahares

Well Known Member
Licensed User
I hope @Myron and @KMatle do not mind if I carry this thread a little further:
I use KMatle's code and follow it up with a list as a final result where the integers can be sorted asc or desc, without having to deal with the NULL in the map values:
B4X:
Log(CreateNumbersList (10, 0, 1000, True))
B4X:
Sub CreateNumbersList (NumbersToCreate As Int, MinNumber As Int, MaxNumber As Int, ascend As Boolean) As List
    Dim NumbersMap As Map
    NumbersMap.Initialize       
    Do Until NumbersMap.size=NumbersToCreate
        NumbersMap.Put(Rnd(MinNumber,MaxNumber+1),Null)
    Loop 
    Dim MyList As List
    MyList.Initialize
    For i=0 To NumbersMap.Size -1
        MyList.Add(NumbersMap.GetKeyAt(i))
    Next
    MyList.Sort(ascend)
    Return MyList
End Sub
 

stevel05

Expert
Licensed User
A variation on a theme :
B4X:
Sub CreateNumbersList(NumbersToCreate As Int,MinNumber As Int,MaxNumber As Int,Ascend As Boolean) As List
    Dim Results As List
    Results.Initialize

    If MaxNumber - MinNumber < NumbersToCreate Then Return Results 'Otherwise it will never finish
   
    Dim NumMap As Map
    NumMap.Initialize
    NumMap.Initialize
    Dim i As Int
    For i = MinNumber To MaxNumber
        NumMap.Put(i,i)
    Next
   
    Do While Results.Size < NumbersToCreate
        Dim O As Object = NumMap.Remove(Rnd(MinNumber, MaxNumber + 1))
        If O <> Null Then Results.Add(O)
    Loop
   
    Results.Sort(Ascend)
   
    Return Results
End Sub
 

Myr0n

Active Member
Licensed User
Everybody are awesome

I like threads like this where we share and learn :D
 
Top