Produce Random Numbers
"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.
|