Android Question how to remember numbers

jchal

Active Member
Licensed User
Longtime User
i have the following simple code
B4X:
sub  ranno()
Dim n=Rnd (1,30) As Int
end sub
i want to remeber the number every time the above code is executed , for example the first time it give me a rundom number 9, how can i store it so next time it is executed i will not take it the number 9 into account and force it to take another number?
 

Star-Dust

Expert
Licensed User
Longtime User
Try writing it in a piece of paper and you will remember it.

joking, try this:
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim L As List
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("Layout1")
    L.Initialize
End Sub

Sub Random As Int
    Dim N As Int = (Rnd (0,30) +1) ' Numerber range 1-30

    If l.Size<30 Then
        Do While L.IndexOf(N)>-1
            N  = (Rnd (0,30) +1) ' Numerber range 1-30
        Loop
        Else
            ' All number find, restart
            L.Clear
        End If
   
    L.Add(N)
    Return N
End Sub
 
Last edited:
Upvote 0

udg

Expert
Licensed User
Longtime User
You have several options.
One could be to use the random number as a key to a map where the value changes from false to true; this way you just check the map "record" to know whether the newly extracted number is valid or not.
Another way could be to fill a list of extracted numbers; if the newly extracted number is in the list...well, you know the answer
Another option, prepare a string of 30 Xs; when a number is extracted you change the corresponding X to something else.
And so on
 
Last edited:
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Second solution:
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim N(30) As Boolean
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("Layout1")
    For I=0 To 29
        N(I)=False
    Next
End Sub

Sub Random As Int
    Dim Count As Int = 0
    Dim Nu As Int = Rnd(0,30)
 
    For i=0 To 29
        If N(i)=True Then Count=Count+1
    Next
 
    If Count<30
        Do While N(Nu)
            Nu = Rnd(0,30)
        Loop
        N(Nu)=True
        Return (Nu+1)
    Else
        ' All number out
        Return 0
    End If
 
End Sub
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
It's too hard. Be simpler
It's the best way; you should not use loops to try/check values "waiting" for a right value.

As I said that is not "farina del mio sacco" (my method) but a classic algorithm.

It simply consists of creating an Array (or a List), and once you pick an element randomly, move this element to the bottom of the array so that it can no longer be reached by the next Rnd operation:

 
Last edited:
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I've written a class call cReplay where instead of call Rnd I call cReplay.GetRnd( same parms )

cReplay will / can write all the requests for Random numbers to a file that later can be returned or replayed.

I use this in my program that uses random number to generate player seating. If the user reports a problem to me that seating is not right through my support dialog it will send me there database and the replay file and I can replay the exact seating (getting the same random number they got during seating) and see what is going on

Attaching a copy of cReplay.bas if this helps anyone


cReplay needs a Global in Starter called gDirectoryApp which is where it will read or write the replay file

------------------------------------------------

I realize after re-reading the initial post that my answer may not apply

To take in to account the last number used why not just save the last number to a file and re-read it when you start next time or next time you call the routine.
 

Attachments

  • cReplay.bas
    4.6 KB · Views: 193
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…