I need to generate a few sets of random numbers and a minimal working example of my code is as follows:
Sub Macro1()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Dim RA1 As Variant
Dim RA2 As Variant
Dim RA3 As Variant
Dim RA4 As Variant
ReDim RA1(1 To 63)
ReDim RA2(1 To 63)
ReDim RA3(1 To 63)
ReDim RA4(1 To 63)
For i = 1 To 1000
Rnd (-1)
Randomize i
For j = 1 To 63
RA1(j) = Rnd
Next j
Rnd (-2)
Randomize i
For k = 1 To 63
RA2(k) = Rnd
Next k
Rnd (-3)
Randomize i
For l = 1 To 63
RA3(l) = Rnd
Next l
Rnd (-4)
Randomize i
For m = 1 To 63
RA4(m) = Rnd
Next m
With Sheets("Economic Assumptions")
.Range("B10:BL10").Value = RA1
.Range("B11:BL11").Value = RA2
.Range("B12:BL12").Value = RA3
.Range("B13:BL13").Value = RA4
.Calculate
End With
Next i
End Sub
However, while RA1 is unique, I find that my random numbers in RA2, RA3 and RA4 are all exactly the same within each i
. In other words, my code gives me RA1 <> RA2 = RA3 = RA4. Why is that? I thought that changing the argument in RND
will change the seed of the random numbers? In particular, I need RA1, RA2, RA3 and RA4 each have their own set of random numbers for each i, but when I re-run the entire thing, I should be getting the same random numbers each time. How can I tweak my code to achieve this?
Edit
After some brainstorming and guess-and-check work, the following code does the trick!
Sub Macro1()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Dim RA1 As Variant
Dim RA2 As Variant
Dim RA3 As Variant
Dim RA4 As Variant
ReDim RA1(1 To 63)
ReDim RA2(1 To 63)
ReDim RA3(1 To 63)
ReDim RA4(1 To 63)
For i = 1 To 1000
Rnd (-1)
Randomize i
For j = 1 To 63
RA1(j) = Rnd
Next j
For k = 1 To 63
RA2(k) = Rnd
Next k
For l = 1 To 63
RA3(l) = Rnd
Next l
For m = 1 To 63
RA4(m) = Rnd
Next m
With Sheets("Economic Assumptions")
.Range("B10:BL10").Value = RA1
.Range("B11:BL11").Value = RA2
.Range("B12:BL12").Value = RA3
.Range("B13:BL13").Value = RA4
.Calculate
End With
Next i
End Sub
It is much shorter and sweeter too. I am so happy :)