Random Numbers with no repeat

brianfosterblack

Active Member
Joined
Nov 1, 2011
Messages
251
I have a macro which allows me to allocate a random number between 1 and 100 between cells A3 and DV3. The random number selected is never repeated in row 3. The iteration is currently set to only 1 row but the macro currently allows me to add more rows by changing the iteration (I do not need this feature for the change I need)
I need a macro that does the same but puts the results into Column A row 1 to 100

Can anyone help please? Here is the code - No problem if another simpler solution is available rather than changing this code.

Option Explicit
Option Base 1
Public jj As Long
Public Const iteration = 1

'***********************************************************************
'* Resampling Process *
'***********************************************************************

Sub Resample()
Dim i As Long
Dim hold(100) As Single, Hold2(100) As Single
Randomize

For i = 1 To 100
Hold2(i) = i
Next i
For jj = 1 To iteration
For i = 1 To 100
hold(i) = Rnd
Next i
Call DoubleSort(100, hold, Hold2)
For i = 1 To 100
Cells(jj + 2, i) = Hold2(i)
Next i
Next jj
End Sub

'***********************************************************************
'* Sorting Process - Sort array y based on array x *
'***********************************************************************

Sub DoubleSort(n As Long, x() As Single, y() As Single)
Dim xTemp As Double
Dim yTemp As Double
Dim i As Long
Dim j As Long

For j = 2 To n
xTemp = x(j)
yTemp = y(j)
For i = j - 1 To 1 Step -1
If (x(i) <= xTemp) Then GoTo 10
x(i + 1) = x(i)
y(i + 1) = y(i)
Next i
i = 0
10 x(i + 1) = xTemp
y(i + 1) = yTemp
Next j

End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Try this

Code:
Sub RandomNumbers()
    Dim n As New Collection, i As Long, ale As Long
    For i = 1 To 100
        n.Add i
    Next
    For i = 1 To 100
        ale = WorksheetFunction.RandBetween(1, n.Count)
        Cells(i, "A") = n(ale)
        n.Remove ale
    Next
    MsgBox "Done"
End Sub
 
Last edited:
Upvote 0
This works perfectly.
While you will not be able to see or sense the difference for only 100 values, the following macro registers (on my computer at least) as slightly more efficient than the code posted earlier...
Code:
Sub RandomNumbers2()
  Dim X As Long, Idx As Long, Tmp As Variant, Arr As Variant
  Arr = [ROW(1:100)]
  For X = 100 To 1 Step -1
    Idx = WorksheetFunction.RandBetween(1, X)
    Tmp = Arr(Idx, 1)
    Arr(Idx, 1) = Arr(X, 1)
    Arr(X, 1) = Tmp
  Next
  [A1:A100] = Arr
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top