Produce Random Numbers
On Jul 31, 1:18*am, "joeu2004" wrote:
"Paul Black" wrote:
So basically, the program will produce two sets of
random numbers without repetition:-
The first being 5 numbers from 50 numbers.
The second being 2 numbers from 9 numbers.
The 5 numbers will be output in cells A1:E1 and the
2 numbers will be output in cells G1:H1.
Try the following....
-----
Randomize
For j = 1 To nComb
* * For h = 1 To nFromMain
* * * * myMain(h) = h
* * Next h
* * n = nFromMain
* * For k = 1 To nDrawnMain
* * * *h = Int(n * Rnd) + 1
* * * *Range("a1").Offset(j - 1, k - 1) = myMain(h)
* * * *If h < n Then myMain(h) = myMain(n)
* * * *n = n - 1
* * Next k
* * For h = 1 To nFromLucky
* * * *myLucky(h) = h
* * Next
* * n = nFromLucky
* * For k = 1 To nDrawsLucky
* * * *h = Int(n * Rnd) + 1
* * * *Range("a1").Offset(j - 1, nDrawnMain + k) = myLucky(h)
* * * *If h < n Then myLucky(h) = myLucky(n)
* * * *n = n - 1
* * Next
Next j
-----
One comment....
You might prefer to use Evaluate("RAND()") instead of Rnd. *Excel RAND has
better random characteristic than VBA Rnd, especially starting in XL2010.
Evaluate("RAND()") is much slower to execute from VBA. *But that might not
be noticable is you generating only 100 or fewer combinations.
If you choose to use Evaluate("RAND()"), the Randomize statement becomes
useless.
PS: *If you use Rnd, the Randomize statement only needs to be done once..
Thanks joeu2004,
Thats exactly what I was after.
I am using Excel 2007 but will have a look at Evaluate("RAND()") as
you suggest and try to adapt the code to see if it makes any
difference.
I tried to run the program using only say 5 numbers from 50 and
exclude producing the second set of numbers and it gave me a ...
Run-time error '9'
Subscript out of range
.... I managed to get around this by commenting out ...
ReDim myLucky(1 To nFromLucky)
Thanks again, it is much appreciated.
Kind regards,
Paul
|