Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 829
Default Unique Random Numbers

"Paul Black" wrote:
On Sep 10, 2:24 pm, "joeu2004" wrote:
As you requested, that macro might generate an irregular
combination -- i.e. fewer than normal -- if nPool is not
an exact multiple of nCol.


If you reach a point where you realize that you would prefer
to avoid the irregular combination (I would), use the
following macro instead.

[....]
I will try and adapt your macro so that when I use 49 numbers
(or whatever) and 6 number combinations that instead of giving
me just 8 lines of 6 numbers that it gives me 8 lines of 6
numbers and 1 line of 1 number.


No adaptation is needed. You are using the wrong one of the __two__ macros
that I posted.

Since you do indeed want the irregular combination, you should use the first
version that I posted.

I will repost it below. Sorry for the confusion.

-----

Option Explicit

Sub Shuffle()

' ***** customize*****
Const rAddress As String = "b2"
Const clrAddress As String = "b:k"
' *****

Dim i As Long, j As Long
Dim nPool As Long, nCol As Long, nRow As Long
Dim r As Range

Randomize

nPool = Application.InputBox("How Many Numbers Would You " & _
"Like To Randomize?", "Shuffle Size", Type:=1)
If nPool <= 0 Then End

nCol = Application.InputBox("How Many Numbers In Each " & _
"Combination?", "Combination Size", Type:=1)
If nCol <= 0 Then End

' determine range of output.
If nCol nPool Then nCol = nPool
nRow = Int((nPool + nCol - 1) / nCol) ' round up
Set r = Range(rAddress).Resize(nRow, nCol)

' clear any previous data
Columns(clrAddress).ClearContents

' initialize pool of numbers for random drawings
ReDim num(1 To nPool) As Long
For i = 1 To nPool: num(i) = i: Next

For i = 1 To nPool
' draw next random number.
' store into range, across columns first,
' then down rows
j = 1 + Int(nPool * Rnd())
r(i) = num(j)

' remove num(j) from pool of numbers
If j < nPool Then num(j) = num(nPool)
nPool = nPool - 1
Next

End Sub

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unique Random Numbers CJ Excel Discussion (Misc queries) 5 February 10th 10 09:13 AM
Unique RANDOM NUMBERS within specified range Matt D Francis Excel Worksheet Functions 5 July 15th 08 08:04 PM
Unique Random Numbers Joe Schmo Excel Programming 2 April 27th 08 04:52 PM
generate unique random numbers Stephen Larivee New Users to Excel 7 March 29th 06 01:04 AM
unique random numbers einemanw Excel Programming 4 January 28th 04 02:46 PM


All times are GMT +1. The time now is 07:26 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"