Android Question (Solved) Random number only used one time

MikeSimpson

Member
Licensed User
Longtime User
I want to create a random number, but the number should be used only one time so that a number don't turn up twice.

I made a working sample with 6 random numbers.

In code row nr. 63 I check if the number I already used turn up again:
B4X:
If rndNr = usedNr(1) OR rndNr = usedNr(2) OR rndNr = usedNr(3) OR rndNr = usedNr(4) OR rndNr = usedNr(5) OR rndNr = usedNr(6) Then

(Attached is the sample program)

Is there a smater (and shorter) way to check for used numbers? Because in my app I need to check 44 numbers.

All the code:

B4X:
Sub Globals
   
    Private btnRndNr As Button
    Private labRndNr As Label
   
    Dim rndNr As Int
    Dim usedNr(7) As Int
    Dim count As Int
    count = 1
   
    Private labCount As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   
    Activity.LoadLayout("Main")

   
End Sub


Sub btnRndNr_Click
   
    rndNr = Rnd(1, 7)    ' Create random nr. 1 to 6
   
                        ' If random nr. was used already, start again
    If rndNr = usedNr(1) OR rndNr = usedNr(2) OR rndNr = usedNr(3) OR rndNr = usedNr(4) OR rndNr = usedNr(5) OR rndNr = usedNr(6) Then
   
    btnRndNr_Click
   
   
    Else                ' Else display random nr.
    labRndNr.Text = "Rnd Nr. = " & rndNr   
   
    usedNr(count) = rndNr    'usedNr get value
   
    'counter
    labCount.Text = "Count = " & count
    count = count +1
   
   
    If count = 7 Then        'If counter is 7, reset to 1 and set usedNr to 0
        count = 1
       
    For i = 0 To 6
        usedNr(i) = 0
    Next

    End If
    End If
End Sub
 

Attachments

  • Random_Test.zip
    7.4 KB · Views: 195

sorex

Expert
Licensed User
Longtime User
you could append the random numbers to a string.

generate the next random one
while rnums.contains(r)
generate another one
loop
append again to rnums

or do you prefer real code? :)
 
Upvote 0

MikeSimpson

Member
Licensed User
Longtime User
Thank you, but as I am a beginner, I prefer real code.:)
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
here you go...

B4X:
Sub Globals
   
   Private btnRndNr As Button
   Private labRndNr As Label
   
   Dim rndNr As Int
   Dim usedNr(7) As Int
   Dim count As Int
   count = 1
   
   Private labCount As Label
   
   Dim rnums As String   ' <- change!!!
End Sub

Sub Activity_Create(FirstTime As Boolean)
   
   Activity.LoadLayout("Main")

   rnums=","  ' <- change!!!
End Sub

Sub btnRndNr_Click
    rndNr = Rnd(1, 42)    ' Create random nr. 1 to 6
    Do While rnums.Contains("," & rndNr & ",")
    rndNr = Rnd(1, 42)
    Loop

    rnums=rnums & rndNr & ","

    Log (rndNr & " > " & rnums)
End Sub

the only negative part about this code is that it might run in an infinite loop when all numbers have been picked.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
if it is a lotto generator then it's no problem. just make sure you set rnums="," at each generation.
 
Upvote 0

MikeSimpson

Member
Licensed User
Longtime User
Thank you.
I have to declare "rnums As String", right?

I think you are right with the fear of the infinite loop.
Isn't it a possibility to use "If .. Then" insted of "Do While .. Loop", similar to my example?

Edit:

You are so fast, will try it.
Many thanks::)
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
I have to declare "rnums As String", right?

yes, notice the ' <- change in the source a few posts back

you can use an if/then indeed but then you either need to define all these 44 items inthere
or wrap it up in a loop to scan values in an array or something.

problem there is when a value matches you can't jump to the top again so you'll end up with more code than what you have now.

that's where the do while/loop comes to the rescue.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
why isn't it random "enough" ?

card shuffleling is another method which was not requested here tho.
 
Upvote 0

MikeSimpson

Member
Licensed User
Longtime User
@eps
It don't have to be 100% random for my propose. I need it to make a quiz-app and I want to cover evenly all questions without missing some and some show up to often. I looked at the result of the code and it looked random enough or me.

@sorex
My last post was before I read your answer (you was to fast;)) and before I understood fully the code you posted.
I implemented your code in my example and it is working fine. Thanks again.

B4X:
Sub btnRndNr_Click

rndNr = Rnd(1, 7)    ' Create random nr. 1 to 6
    Do While rnums.Contains("," & rndNr & ",")
    rndNr = Rnd(1, 7)
    Loop

    rnums=rnums & rndNr & ","

    Log (rndNr & " > " & rnums)
  
    labRndNr.Text = "Rnd Nr. = " & rndNr  
  
        'counter
    labCount.Text = "Count = " & count
    count = count +1
  
  
    If count = 7 Then        'If counter is 7, reset to 1 
        count = 1
        rnums=","
    
    End If

End Sub
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I made this:
B4X:
Sub RndUniqueNums(MinValue As Int, MaxValue As Int, NumOfValues As Int) As List
    Private lstAvailable As List
    lstAvailable.Initialize
    For N = MinValue To MaxValue
        lstAvailable.Add(N)
    Next
 
    Private Available As Int
    Available = MaxValue - MinValue + 1
 
    Private RetList As List
    RetList.Initialize
 
    Private NumDispIdx As Int
    For i=0 To NumOfValues-1
        NumDispIdx = Rnd(1, Available+1)-1
        RetList.Add(lstAvailable.Get(NumDispIdx))
        lstAvailable.RemoveAt(NumDispIdx)
        Available = Available -1
    Next
 
    Return RetList
End Sub

with which you get a list of integers different from each other.

[numbers between MinValue and MaxValue]

[It is old stuff. Looking at it now may be wrong.
I could pass MinValue = 1, MaxValue = 10 and ask for NumOfValues = 20]
 
Upvote 0

MikeSimpson

Member
Licensed User
Longtime User
Thank you LucaMs, but that is to complicated for me at the moment.
 
Upvote 0

eps

Expert
Licensed User
Longtime User
@MikeSimpson no problem, I understood (mostly) what you were attempting to achieve, but just wanted to be clear that we're not really talking about random numbers - you already know that but others might not.

:)
 
Upvote 0
Top