Android Question Random function fails! Solved

Dragobyte

Member
Licensed User
Longtime User
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:

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:

Dragobyte

Member
Licensed User
Longtime User
What is the error that occurs? The code looks OK.
Yes, the code is good. It's no problem of the code.
The problem is how the memory of the phone colapses.
The error is that instead of appear the numbers 1-20 shuffled, appear only 5 numbers repeated four times. And always the same numbers.
Maybe this app works good in many phones, but in my phone easily goes wrong.
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Maybe this app works good in many phones, but in my phone easily goes wrong.
You still do not provide any info about WHAT does not wor or which ERROR you get.

Upload a small project which shoes the issue. Averything else we just can fish.
 
Upvote 0

Dragobyte

Member
Licensed User
Longtime User
Yes, the code is in my message. There is no more code, only the default. Also add the two objects (button and label).
Here is the project.
Thanks


codetag001.png

codetag002.png

codetag003.png
[/QUOTE]
 

Attachments

  • Shufflex.zip
    408.4 KB · Views: 334
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I ran you project on a Samsung Galaxy Tab A with OS 5.1.1 and a Motorola phone and it worked beautifully. I clicked the button many times and encountered no problems. 20 numbers change every time. Try to do the following from the IDE and see if it helps: Clean files folder and clean project, then run it and see what you come up with. I zipped and exported your project and came up with only 10 KB and you show the zipped file above with 408 KB.
 
Upvote 0

Derek Johnson

Active Member
Licensed User
Longtime User
The code that you posted never fails for me.

The error is that instead of appear the numbers 1-20 shuffled, appear only 5 numbers repeated four times. And always the same numbers.

In order to get this behaviour I think that there must be a typo in the code, possibly a mis-typed step size or array size. Is the code that you posted cut and pasted from the failing code?
 
Upvote 0

Dragobyte

Member
Licensed User
Longtime User
Hum, I think it's not a problem of code. Yes, the code it´s exactly the same. The problem must be in my phone and memory.
That's the reason I want any code or command to free memory like the recent apps list does. When I press the recent apps list button (erase all) all goes good until I press 5th time my app button. I have cleaned memory, uninstalled apps, moved to sd memory, use IDE options but always the same.
I have to say that the problem is not only this app, is for every app I use Rnd (in my phone). I have made this little shuffle app to point, to show this problem.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Please edit the first post and add code tags (don't make any other posts before fixing it).

2. A correct shuffle code should work even if Rnd produces the exact same series of numbers each time. If it doesn't work then there is a problem in your code.

3. I don't believe that there is any issue with your phone memory.

4. You can check the output of Rnd with:
B4X:
For i = 1 to 100
Log(Rnd(1, 101))
Next

Run it several times and see whether you get the same series of numbers.
5. Tip: if you want to log an array:
B4X:
Dim list As List = MyArray
Log(list)
 
Upvote 0

Dragobyte

Member
Licensed User
Longtime User
1. Please edit the first post and add code tags (don't make any other posts before fixing it).

2. A correct shuffle code should work even if Rnd produces the exact same series of numbers each time. If it doesn't work then there is a problem in your code.

3. I don't believe that there is any issue with your phone memory.

4. You can check the output of Rnd with:
B4X:
For i = 1 to 100
Log(Rnd(1, 101))
Next

Run it several times and see whether you get the same series of numbers.
5. Tip: if you want to log an array:
B4X:
Dim list As List = MyArray
Log(list)

Thanks Erel, The code is put. Shuffle always works well, the problem is the repetition of numbers. I think code is ok, it has no problem before actualization of B4A version. Other people has no problem but my phone has. I only know that when I press recent apps list button and erase all, the Rnd function goes well for a few times.
It's something to investigate at Anywhere or Sony, I don´t know. I am interested in code that can make the same effect as Recent apps list button to put before Shuffle function.
I have used debug mode and works fine, but installing the app there is problem again.
 
Upvote 0

Dragobyte

Member
Licensed User
Longtime User
Problem solved. Finally.
After testing RndSeed, phone options, etc... with no success, I simply made the next step: I declared i, j and K in Globals.
Specially after changing i to Globals the app works with no problem.
Call me dumb if you want but I thought it was no important where had to be the variable i in this case.
Thank you so much to everybody helping and interested in this post. THANKS!!
 
Upvote 0

Derek Johnson

Active Member
Licensed User
Longtime User
I simply made the next step: I declared i, j and K in Globals

It's usually best to declare loop variables locally. If what you did fixed the problem, then it's probably made some subtle difference to a totally unrelated bit of code, and you've probably still got some weird dependency in your code!
 
Upvote 0

Dragobyte

Member
Licensed User
Longtime User
It's usually best to declare loop variables locally. If what you did fixed the problem, then it's probably made some subtle difference to a totally unrelated bit of code, and you've probably still got some weird dependency in your code!
Yes, I think the same as you.
Thanks
 
Upvote 0
Top