Solved

randomize producing the same number more than once

Posted on 2013-12-20
25
227 Views
Last Modified: 2014-01-02
i  have a 80 number msflexgrid cols 0 to 9 rows 0 to 7
I try to produce 20 randomly selected numbers, but have found the randomize outputs the same numbers more than once sometimes the same number is out put 3-4 times.
The randomize is only called on form load
How do i correct this:

Sub RandomNumbersinGrid() 'put images of selected numbers based on randomized in grid
    Dim DrawNumber As Integer
    Dim NumberAlreadyHit As Integer
     Dim lCol As Integer, lRow As Integer
    Dim i As Integer, thisIndex As Integer, j As Integer
    'Shuffle
     For i = 1 To DRAWN_NUMBERS 'DRAWN_NUMBERS = 20
    thisIndex = Int(80 * Rnd + 1)
    
    CellFromIndex thisIndex, lRow, lCol 'get the row and column number
    Debug.Print "GivenNumber " & thisIndex
    DoEvents
    Sleep 100
    DrawNumber = DrawNumber + 1
     
 With flexCleo
     
            .Row = lRow
            .Col = lCol
            If .CellPicture = imgHitUnSelected.Picture Then' number already hit
            NumberAlreadyHit = NumberAlreadyHit + 1
            Debug.Print "Hit on the same number " & NumberAlreadyHit '& " " & "number " & thisIndex
            End If
          Set .CellPicture = imgHitUnSelected.Picture
          'DrawNumber = DrawNumber + 1
End With
    Next
    Debug.Print "Hit on the same number " & NumberAlreadyHit & " " & "number " & thisIndex
    Me.Caption = DrawNumber
    End Sub


Private Sub CellFromIndex(ByVal pindex As Long, ByRef pRow As Integer, ByRef pCol As Integer)
  'sets the row and column numbers based on the number passed
        pRow = (pindex - 1) \ flexCleo.Cols
    pCol = (pindex - 1) Mod flexCleo.Cols
 
End Sub
 

Open in new window

seeProblem jpg
0
Comment
Question by:isnoend2001
  • 8
  • 8
  • 7
  • +1
25 Comments
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 100 total points
ID: 39732683
If you create a limited number of integer values, it is bound to happen that some numbers will appear more than once.

If you need each one to be unique, you will have to check whether any particular number has already been chosen and to try again if it has.

Also, the 'seed' used to generate the number is fixed when the rnd function is first called, so the same sequence of numbers will be produced for each run. The Randomize function can be called  and use the system time as the seed, so the numbers will not be predictable.
0
 

Author Comment

by:isnoend2001
ID: 39732790
thanks : GrahamSkan
20 different spots have to be produced  with each run
The app also has an auto-run where a different set of 20 numbers is produced with each run.
i don't understand the try again or how to implement it.
In testing almost every run produced duplicate numbers sometime only 15 or 16 spots were selected
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39732796
This is the same as the 'birthday problem'. In a group of only  23 people there is an evens chance of two of them sharing a birthday:

http://en.wikipedia.org/wiki/Birthday_problem
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39732813
Oops. Cross-posted.

If your application starts afresh each time and you don't have a Randomize in the code, then, by design, the sequence of numbers produced will be the same each time. This is to help in debugging.

To avoid that you need:
Randomize
Call RandomNumbersinGrid() 

Open in new window

0
 

Author Comment

by:isnoend2001
ID: 39732838
:Thank GrahamSkan
That would solve the problem for unique numbers with each run, but the problem remains with the randomize producing the same number more than once in a given run
0
 
LVL 45

Accepted Solution

by:
Martin Liss earned 400 total points
ID: 39732853
Here's how to select 20 unique random numbers.

Dim ColHits As New Collection
Dim intHit As Integer

Do Until ColHits.Count = 20
    intHit = Int(80 * Rnd + 1)
    On Error Resume Next
    ' Duplicates will cause an error and be discarded
    ColHits.Add intHit, CStr(intHit)
Loop

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39732870
As the Wiki article points out, coincidences are more likely than intuition would suggest. The Rnd function will, it is true, always give the same answers from any given seed, so it isn't actually random, just unpredictable. You could choose to use the Randomize function before each Rnd, but that is like randomising the random number. However do that if it seems to produce a better result for you.
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 39732913
You could choose to use the Randomize function before each Rnd, but that is like randomising the random number. However do that if it seems to produce a better result for you.
Test have shown that that actually produces less randomization. In short Randomize should be done just once.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39732917
Martin,
I didn't know that, but it doesn't surprise me.
0
 

Author Comment

by:isnoend2001
ID: 39732986
Thanks MartinLiss how would i add your collection code to mine ?
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 39733014
I don't remember what your code looks like, but here's an outline of what you should do.

1. Create this function and call it when you want to generate a new set of 20 random numbers.

Public Sub Generate20Numbers()
Dim ColHits As New Collection
Dim intHit As Integer

Do Until ColHits.Count = 20
    intHit = Int(80 * Rnd + 1)
    On Error Resume Next
    ' Duplicates will cause an error and be discarded
    ColHits.Add intHit, CStr(intHit)
Loop
End Sub

Open in new window


2. Then when you want to see if a selected number (for example 14) is in the collection you would do this. Note that I'm using a hardcoded value but you should determine the value from the fact that the forecolor is yellow and I think we talked about that previously.

Dim lngIndex As long

For lngIndex = 1 to 20
    If ColHits(lngIndex) = "14" Then
        Msgbox "Found"
        Exit For
   End If
Next

Open in new window

0
 

Author Comment

by:isnoend2001
ID: 39733070
Thanks MartinLiss
Using debug on your code produces duplicates see below
Public Sub Generate20Numbers()
Dim ColHits As New Collection
Dim intHit As Integer

Do Until ColHits.Count = 20
    intHit = Int(80 * Rnd + 1)
    On Error Resume Next
    ' Duplicates will cause an error and be discarded
    ColHits.Add intHit, CStr(intHit)
    Debug.Print intHit 'is this not a good test
Loop
End Sub

Debug Results
 71
 12 here 12
 39
 80
 10
 16

I am using Randomize only on form load



 22 here 22
 8
 52
 29 here 29
 63 here 63
 63 here 63
 13
 29 here 29
 36
 7
 49
 12 here 12
 76
 23
 22 here 22
 56
I don't have an issue yet with seeing if a certain number is selected
I will use the code you provided using the cells forecolor
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 45

Expert Comment

by:Martin Liss
ID: 39733076
The code can not produce duplicates. To show the true output do this

Dim ColHits As New Collection
Dim intHit As Integer
Do Until ColHits.Count = 20
    intHit = Int(80 * Rnd + 1)
    On Error Resume Next
    ' Duplicates will cause an error and be discarded
    ColHits.Add intHit, CStr(intHit)
Loop

Dim intIndex As Integer

For IntIndex = 1 to 20
    Debug.Print ColHits(intIndex)
Next
End Sub

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39733078
You would need a much larger experiment to show a true bias from expected statistics
0
 
LVL 45

Expert Comment

by:aikimark
ID: 39734622
1. It would be helpful if you read my analysis of the VB pseudo-random number generation.
http:A_11114.html

The take-away is that collisions/duplicates will happen.

2. In addition to the collection object, you can prevent duplicates with a dictionary object (using its .Exists method) or an array variable.
0
 

Author Comment

by:isnoend2001
ID: 39734832
thanks  aikimark This is not the only problem i have had with this. The other problem set me
back $thousands I would mark lower numbers and run them millions of times and win..I thought i had found patterns that could beat the casinos. I would go to the casinos and always loose $
so i added a feature to track which numbers are hit the most.
Then i determined that the higher numbers i my pc were hit less often  than the lower numbers. I hired a coder to fix the problem and now i loose on my pc just like the casino.
problem is with my limited knowledge of coding i can not determine what he did.
And i cannot find him
In making this new game i hope the problem does not resurface (see jpg)
most-hit-numbers.jpg
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 39734842
I think I can safely say that nobody beats the casinos. You may win a few days in a row, but eventually you will lose. And even if you are successful in finding patterns of "unrandomness" using randomize, it won't help you because it is certain that the casinos don't use the same method for generating their random numbers.
0
 

Author Comment

by:isnoend2001
ID: 39734862
Thanks MartinLiss
Yes,since I corrected the problem i lose at home just like the casinos. They have the odds figured out where they will win
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 39743700
Is there anything more that we can do for you here?
0
 

Author Comment

by:isnoend2001
ID: 39743729
Thanks for asking
Almost posted a few times in the last couple days, but finally overcame a few issues I was stuck on.really stupid things
i am trying to implement some of the hired coders code into my new keno game. Some of his code is beyond me. I will post a question on my current type mismatch problem
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 39744063
I'll help if I can but what I was really asking was if you were ready to close this question.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39744149
Note that some experts here will do off-line work, though there might be a fee involved. Their direct email addresses will be in their profiles

However, be aware that debugging other coders' efforts  could be more difficult than writing from scratch.
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 39751710
You seem to have forgotten this question.
0
 

Author Closing Comment

by:isnoend2001
ID: 39752430
Didn't know this ? was still open
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now