如何从 basic4android 中的数组中提取随机数?

How to draw random number from array in basic4android?

我对 Basic4Android 编程还很陌生。我只想知道如何从数组中提取随机数。

如果项目为 10,则可以通过随机化数组示例中的项目总数来实现,然后先随机化并将其传递给变量。稍后使用该变量来调用数组值。复制下面的代码并粘贴到 Activity Create Sub 和 运行

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)) 
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)