Android Question Choose a random between 4 strings

SMOOTSARA

Active Member
Licensed User
Longtime User
How can I print one of these 4 items by chance??


B4X: Copy
Dim str1() As String
str1 = Array As String("no", "yes","ooo","test","yyy","nnn")
Log(?????????)

? ?
 
B4X: Copy
Dim str As String = Regex.Split(",","no,yes,ooo,test,yyy,nnn")(rnd(0, 6))
 
Upvote 0
Mine is better
This one is even better :D
Code: Copy
Dim str1() As String = Array As String("no", "yes","ooo","test","yyy","nnn")
Log(str1(rnd(0, str1.Length)))
No unused vars (str and Index as from #2 and #3 above) .. :D
 
Upvote 0
This one is even better :D
Code: Copy
Dim str1() As String = Array As String("no", "yes","ooo","test","yyy","nnn")
Log(str1(rnd(0, str1.Length)))
No unused vars (str and Index as from #2 and #3 above) .. :D
I wrote that mine is better precisely because I used the variable Index which, in addition to being explanatory, is also "loggable" ?
 
Upvote 0
If we are tabulating the number of ways, here is another:
Code: Copy
Dim MyList As List
MyList.Initialize
MyList.AddAll(Array ("no", "yes","ooo","test","yyy","nnn"))
Log(MyList.Get(Rnd(0, MyList.Size)))
 
Upvote 0
The second code generates an static collection and therefore it will no longer be possible to add or remove elements
 
Last edited:
Upvote 0
The second code generates an immutable list and therefore it will no longer be possible to add or remove elements
You're quick on the draw today ?
 
Upvote 0
I would add that the array brings together data of the same type, while in the list you could put different data type
 
Last edited:
Upvote 0
I would add that the array brings together data of the same type, while in the list you could put different data type
That's true if the array type is not Object (no type = Object).

No difference to me. They are the same:
Try:
Code: Copy
Dim MyList As List = Array ("no", "yes","ooo","test","yyy","nnn")
MyList.Add("Mahares")
 
Upvote 0
Try:
B4X: Copy
Dim MyList As List = Array ("no", "yes","ooo","test","yyy","nnn")
MyList.Add("Mahares")
I was under the impression that when you use : MyList.set(.., you are actually removing and adding at the same time to come up with the 'replace'. Apparently, that is not the case with list.
 
Upvote 0
Back
Top