title: Type mismatch error when generating random numbers
body: <p>When I generate random numbers, I sometimes get (not always) the following error: </p>
  
  <blockquote>
    <p>Run-time error '13': type mismatch.</p>
  </blockquote>
  
  <p>on line <code>Z = Sqr(time) * Application.NormSInv(Rnd())</code> (and the end of the second for loop). </p>
  
  <p>Why do I get this error?</p>
  
  <p>I think it has something to do with the fact that it contains <code>Rnd()</code>. </p>
  
  <pre><code>Sub asiancall()
  'defining variables
  Dim spot As Double
  Dim phi As Integer
  Dim rd_cont As Double
  Dim rf_cont As Double
  Dim lambda As Double
  Dim muY As Double
  Dim sigmaY As Double
  Dim vol As Double
  
  Dim implied_vol As Double
  Dim spotnext As Double
  Dim time As Double
  Dim sum As Double
  Dim i As Long
  Dim mean As Double
  Dim payoff_mean As Double
  Dim StDev As Double
  Dim K As Double
  Dim Egamma0 As Double
  Dim mulTv As Double
  Dim prod As Double
  Dim U As Double
  Dim Pois As Double
  Dim Q As Double
  Dim Z As Long
  Dim gamma As Double
  Dim payoff As Double
  Dim payoff_sum As Double
  Dim secondmoment As Double
  Dim j As Long
  Dim N As Long
  Dim mu As Double
  Dim sum1 As Double
  
  'read input data
  spot = Range("B3")
  rd_cont = Range("C5")
  rf_cont = Range("C4")
  muY = Range("B17")
  sigmaY = Range("B18")
  lambda = Range("B16")
  K = Range("F33")
  implied_vol = Range("F35")
  N = Range("F34")
  vol = Range("B6")
  'calculations
  
   sum_BS = 0
   payoff_BS = 0
   mean_BS = 0
   secondmoment_BS = 0
   For j = 1 To N
       spotnext = spot
       spotnext_BS = spot
       time = 0
       sum1 = 0
       time = 184 / (360 * 6)
       For i = 1 To 6
  
      '        'Merton uitvoeren
           Egamma0 = Exp(muY + sigmaY * sigmaY * 0.5) - 1
           mu = rd_cont - rf_cont
           mulTv = (mu - lambda * Egamma0 - implied_vol * implied_vol * 0.5) *   time
           sum = 0
           prod = 1
           Do While sum &lt;= time
               U = Rnd()
               Pois = -Log(U) / lambda
               sum = sum + Pois
               Q = Application.NormInv(Rnd(), muY, sigmaY)
               gamma = Exp(Q) - 1
               prod = prod * (1 + gamma)
           Loop
           prod = prod / (1 + gamma)
           Z = Sqr(time) * Application.NormSInv(Rnd())
           spotnext = spotnext * Exp(mulTv + implied_vol * Z) * prod
           sum1 = sum1 + spotnext
       Next i
       mean = sum1 / 6
       payoff = Application.Max(mean - K, 0)
       payoff_sum = payoff_sum + payoff
       secondmoment = secondmoment + payoff * payoff
  Next j
  </code></pre>