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:
Dim str1() As String
str1 = Array As String("no", "yes","ooo","test","yyy","nnn")
Log(?????????)

? ?
 

Star-Dust

Expert
Licensed User
Longtime User
B4X:
Dim str As String = Regex.Split(",","no,yes,ooo,test,yyy,nnn")(rnd(0, 6))
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Mine is better
This one is even better :D
B4X:
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

Mahares

Expert
Licensed User
Longtime User
If we are tabulating the number of ways, here is another:
B4X:
Dim MyList As List
MyList.Initialize
MyList.AddAll(Array ("no", "yes","ooo","test","yyy","nnn"))
Log(MyList.Get(Rnd(0, MyList.Size)))
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
The second code generates an static collection and therefore it will no longer be possible to add or remove elements
 
Last edited:
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
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

Star-Dust

Expert
Licensed User
Longtime User
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

Erel

B4X founder
Staff member
Licensed User
Longtime User
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:
B4X:
Dim MyList As List = Array ("no", "yes","ooo","test","yyy","nnn")
MyList.Add("Mahares")
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Try:
B4X:
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
Top