isnoend2001
asked on
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:
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
see jpg
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
http://en.wikipedia.org/wiki/Birthday_problem
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:
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()
ASKER
: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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
Martin,
I didn't know that, but it doesn't surprise me.
I didn't know that, but it doesn't surprise me.
ASKER
Thanks MartinLiss how would i add your collection code to mine ?
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.
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.
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
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
ASKER
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
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
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
You would need a much larger experiment to show a true bias from expected statistics
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.
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.
ASKER
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
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
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.
ASKER
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
Yes,since I corrected the problem i lose at home just like the casinos. They have the odds figured out where they will win
Is there anything more that we can do for you here?
ASKER
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
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
I'll help if I can but what I was really asking was if you were ready to close this question.
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.
However, be aware that debugging other coders' efforts could be more difficult than writing from scratch.
You seem to have forgotten this question.
ASKER
Didn't know this ? was still open
ASKER
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