I have a Monte Carlo simulation in VBA. The client wants (dont question why) to fix random number sequence, i.e. every time you run the model, sequence shall stay the same. I managed to fix random seed as described here. BUT it is not the same on different PCs. Any idea why and how can I also fix it on different machines?
Asked
Active
Viewed 3,123 times
2
-
1Why not just create a sequence of random numbers and save the sequence for use in your application? – whytheq Apr 07 '14 at 09:43
-
1And this is perfectly logical - the clients want the Monte Carlo simulation to be repeatable. – brettdj Apr 07 '14 at 09:54
-
Or you could, using **VBA** , code your own pseudo-random number generator. – Gary's Student Apr 07 '14 at 11:00
-
@whytheq: I find that too gimmicky, and not too efficient – NeatNerd Apr 07 '14 at 11:18
-
@Gary'sStudent If have an example of code in VBA for random number generation I would be very thankful to u – NeatNerd Apr 07 '14 at 11:19
-
possible duplicate of [Repeating random variables in VBA](http://stackoverflow.com/questions/2884972/repeating-random-variables-in-vba) – brettdj Apr 08 '14 at 01:59
-
yeah considering how it turned out, you are probably right. – NeatNerd Apr 08 '14 at 09:25
2 Answers
3
You can use the rnd
function with a negative argument to achieve a repeating list of random numbers.
Here is a link to the documentation:
http://office.microsoft.com/en-us/access-help/rnd-function-HA001228901.aspx
Note To repeat sequences of random numbers, call Rnd with a negative argument immediately before using Randomize with a numeric argument. Using Randomize with the same value for number does not repeat the previous sequence.
Sub TestRandomNumberSequence()
rnd (-10)
For i = 1 To 5
Randomize 10
MsgBox BetweenRange(1, 20, rnd)
Next i
'always returns the following sequence
'5
'18
'19
'6
'17
End Sub
Function BetweenRange(min As Integer, max As Integer, ByVal rnd As Double) As Integer
BetweenRange = Int((max - min + 1) * rnd + min)
End Function

Sam
- 7,245
- 3
- 25
- 37
-
Hmm, true, but strange, that I get different number on different computers, must be error in my code. Thx – NeatNerd Apr 07 '14 at 13:57
-
This is the same answer already included in http://stackoverflow.com/questions/2884972/repeating-random-variables-in-vba – brettdj Apr 08 '14 at 01:59