Android Code Snippet RandomStuff() - Improved Randomness (B4A/B4J/B4i)

Welcome to the future! :D
B4X:
'Return a random object from the specified object array.
Sub RandomStuff(ObjectArray() As Object) As Object
    Return ObjectArray(Rnd(0, ObjectArray.Length))
End Sub

Usage (B4J):

Before:
B4X:
...
Dim Color As Paint
Dim c = Rnd(0, 5) as int
Select c
    Case 0
        Color = fx.Colors.Red
    Case 1
        Color = fx.Colors.Green
    Case 2
        Color = fx.Colors.Blue
    Case 3
        Color = fx.Colors.Yellow
    Case 4
        Color = fx.Colors.Gray
End Select
cvs.DrawCircle(300, 200, 100, Color, True, 1)
...

Now:
B4X:
    Dim Color() As Object
Color = Array As Object(fx.Colors.Red, fx.Colors.Green, fx.Colors.Blue, fx.Colors.Yellow, fx.Colors.Gray)
cvs.DrawCircle(300, 200, 100, RandomStuff(Color), True, 1)

I hope the code is self-explanatory. :)
 
Last edited:
Top