View Single Post
  #12   Report Post  
Posted to microsoft.public.excel.programming
Paul Black Paul Black is offline
external usenet poster
 
Posts: 394
Default Unique Random Numbers

On Sep 8, 9:05*pm, "joeu2004" wrote:
I wrote:
"Paul Black" wrote:
Unfortunately the UDF produces replica numbers.
What I would prefer is a Sub that produces 5,6,7
or whatever number combinations without replacement
until ALL the numbers have been used.


It does exactly that if you put =uniqRandBetween(1,34)
into 34 different cells in any arrangement that you wish.
(Previously, you mention 6 rows of 5 and 1 row of 4.)


After populating the 34 cells as your desire, you can generate a new set of
34 unique random values by pressing ctrl+alt+F9.

Alternatively, you could execute the following macro, changing the cell
arrangement as you wish.

Sub genAll()
Dim i as Long, j as Long
Randomize
For i = 1 to 6
* * For j = 1 to 5
* * * *Cells(i,j) = uniqRandBetween(1,34)
* * Next
Next
For j = 1 to 5
* * Cells(7,j) = uniqRandBetween(1,34)
Next
End Sub


Thanks for your reply Joe.
I have been surfing the web and have come to the conclusion that I
have probably been asking completely the wrong question.
It appears that I need a Sub that "Shuffles" an array.
Unfortunately I can't find one that accommodates exactly what I am
after, which is basically being able to designate the size of the
array (e.g. 34 numbers or 40 numbers or 45 numbers etc) and then
choose whether I want them listed as 4 number combinations, 5 number
combinations or 6 number combinations for example starting in cell
"B2".
If you have any code that does this I would be grateful.

Thanks,
Paul