I am very new to Basic4Android programming. I just want to know how to draw a random number from an array.
Asked
Active
Viewed 1,043 times
2 Answers
0
You can do that by randomizing the total number of items in array example if the items are 10, then randomize first and pass it to a variable. later use that variable to call the array value. Copy the code below and paste in Activity Create Sub and Run
Dim myArray As List ' Declare your Array
myArray.Initialize() ' Initialize array
myArray.AddAll(Array As String("January","February","March","April"))
'Since array values index starts from zero, then four items in a list will be from 0 to 3.
'So randomize 0 to 4
Dim randNum As Int
randNum = Rnd(1,4) 'Generating random number
Log ("Current RAndom Number is " & randNum) 'This will print the random number
'=========PRINT RESULT TO LOGCAT ======
'Since we are generating from 1 to 4, we use -1 (4-1=3 ie April ==Array index starts from 0 to 3)
Log(myArray.Get(randNum-1))

Michał Perłakowski
- 88,409
- 26
- 156
- 177

boluvisako
- 51
- 6
-
Thanks! It helped me a lot! :) – Marion Balba Jan 23 '16 at 23:22
-
Am glad I was able to help – boluvisako Jan 25 '16 at 09:34
0
Dim arrayLength as int = 100 ' an arbitrary integer >0
Dim myArray(arrayLength) as int ' or double, float, long, byte...
' ... fill the array ...
Log(myArray(Rnd(0,arrayLength))) ' "Rnd" goes from 0 (incl) to arrayLength (excl)

Matti81
- 216
- 3
- 3