2

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?

7
  • 1
    Why not just create a sequence of random numbers and save the sequence for use in your application? Commented Apr 7, 2014 at 9:43
  • 1
    And this is perfectly logical - the clients want the Monte Carlo simulation to be repeatable. Commented Apr 7, 2014 at 9:54
  • Or you could, using VBA , code your own pseudo-random number generator. Commented Apr 7, 2014 at 11:00
  • @whytheq: I find that too gimmicky, and not too efficient Commented 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 u Commented Apr 7, 2014 at 11:19

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Hmm, true, but strange, that I get different number on different computers, must be error in my code. Thx
This is the same answer already included in stackoverflow.com/questions/2884972/…
1

As per your request, please checkout the following link:

Wabash College Download

1 Comment

Thx for that too, but it uses RND method from VBA too I think

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.