Hello everybody!
Recently the classic routines that I used for many applications to shuffle numbers is failing, repeating numbers with no sense.
I made a little app to point this. In this app, ALWAYS there is error when I click the fith time the button, and always are the same numbers repeated in the error (8, 12, 20, 16, 4).
When I free memory with the button of the phone it goes ok until I press 5th time.
With other apps there are the same problem with repetitions.
How can I fix this? Is there any command to free memory? How can I improve (with nanoseconds) the Rnd function?
Thanks
Code used for shuffle example app:
Recently the classic routines that I used for many applications to shuffle numbers is failing, repeating numbers with no sense.
I made a little app to point this. In this app, ALWAYS there is error when I click the fith time the button, and always are the same numbers repeated in the error (8, 12, 20, 16, 4).
When I free memory with the button of the phone it goes ok until I press 5th time.
With other apps there are the same problem with repetitions.
How can I fix this? Is there any command to free memory? How can I improve (with nanoseconds) the Rnd function?
Thanks
Code used for shuffle example app:
B4X:
#Region Project Attributes
#ApplicationLabel: Shuffle
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Private Button1 As Button
Private Label1 As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("Shuff1")
End Sub
Sub Shuffle(arr() As Int)
For i = arr.Length - 1 To 0 Step -1
Dim j, k As Int
j = Rnd(0, i + 1)
k = arr(j)
arr(j) = arr(i)
arr(i) = k
Next
End Sub
Sub Button1_Click
Dim nu(20) As Int
For i = 0 To 19
nu(i) = i + 1
Next
'Shuffle(nu)
For i = nu.Length - 1 To 0 Step -1
Dim j, k As Int
j = Rnd(0, i + 1)
k = nu(j)
nu(j) = nu(i)
nu(i) = k
Next
For i = 0 To 19
Log(nu(i)) 'print the numbers to the log
Next
Dim stli As String
stli=nu(0) & ", " & nu(1) & ", " & nu(2) & ", " & nu(3) & ", " & nu(4) & ", " & nu(5) & ", " & nu(6) & ", " & nu(7) & ", " & nu(8) & ", " & nu(9) & ", " & nu(10) & ", "& nu(11) & ", " & nu(12) & ", " & nu(13) & ", " & nu(14) & ", " & nu(15) & ", " & nu(16) & ", " & nu(17) & ", " & nu(18) & ", " & nu(19)
Label1.Text=stli
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Last edited: