Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

randomize producing the same number more than once

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
isnoend2001
Asked:
isnoend2001
  • 8
  • 8
  • 7
  • +1
2 Solutions
 
GrahamSkanCommented:
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
 
isnoend2001Author Commented:
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
 
GrahamSkanCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
GrahamSkanCommented:
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
 
isnoend2001Author Commented:
: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
 
Martin LissRetired ProgrammerCommented:
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
 
GrahamSkanCommented:
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
 
Martin LissRetired ProgrammerCommented:
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
 
GrahamSkanCommented:
Martin,
I didn't know that, but it doesn't surprise me.
0
 
isnoend2001Author Commented:
Thanks MartinLiss how would i add your collection code to mine ?
0
 
Martin LissRetired ProgrammerCommented:
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
 
isnoend2001Author Commented:
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
 
Martin LissRetired ProgrammerCommented:
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
 
GrahamSkanCommented:
You would need a much larger experiment to show a true bias from expected statistics
0
 
aikimarkCommented:
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
 
isnoend2001Author Commented:
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
 
Martin LissRetired ProgrammerCommented:
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
 
isnoend2001Author Commented:
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
 
Martin LissRetired ProgrammerCommented:
Is there anything more that we can do for you here?
0
 
isnoend2001Author Commented:
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
 
Martin LissRetired ProgrammerCommented:
I'll help if I can but what I was really asking was if you were ready to close this question.
0
 
GrahamSkanCommented:
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
 
Martin LissRetired ProgrammerCommented:
You seem to have forgotten this question.
0
 
isnoend2001Author Commented:
Didn't know this ? was still open
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 8
  • 7
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now