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?
-
1Why not just create a sequence of random numbers and save the sequence for use in your application?whytheq– whytheq2014-04-07 09:43:51 +00:00Commented Apr 7, 2014 at 9:43
-
1And this is perfectly logical - the clients want the Monte Carlo simulation to be repeatable.brettdj– brettdj2014-04-07 09:54:58 +00:00Commented Apr 7, 2014 at 9:54
-
Or you could, using VBA , code your own pseudo-random number generator.Gary's Student– Gary's Student2014-04-07 11:00:35 +00:00Commented Apr 7, 2014 at 11:00
-
@whytheq: I find that too gimmicky, and not too efficientNeatNerd– NeatNerd2014-04-07 11:18:33 +00:00Commented Apr 7, 2014 at 11:18
-
@Gary'sStudent If have an example of code in VBA for random number generation I would be very thankful to uNeatNerd– NeatNerd2014-04-07 11:19:18 +00:00Commented Apr 7, 2014 at 11:19
|
Show 2 more comments
2 Answers
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
2 Comments
NeatNerd
Hmm, true, but strange, that I get different number on different computers, must be error in my code. Thx
brettdj
This is the same answer already included in stackoverflow.com/questions/2884972/…
As per your request, please checkout the following link:
1 Comment
NeatNerd
Thx for that too, but it uses RND method from VBA too I think