jeudi 22 mars 2018

Linear Congruential Generator on VBA

i'm trying to replicate the Linear Congruential Generator in VBA but my procedure returns to me an Error '6': Overflow...

Dim a, c, period, seed As Long
Dim sample, max, number As Integer
Dim i As Integer


Sheets("RNG").Activate

seed = Range("B1").Value
sample = Range("B2").Value
max = Range("B3").Value

a = 48271
c = 0
period = 2 ^ 31 - 1

For i = 1 To sample
 seed = (a * seed + c) Mod period

 number = Int((seed / period) * max + 1)

 Range("E1").Offset(i, 0).Value = i
 Range("F1").Offset(i, 0).Value = number

Next i

The procedure should take the seed and the algorithm's parameters from excel, in detail I have error with:

seed = 1234
sample = 2 (size of sample)
max = 100 (max of the range, the procedure takes numbers from 1 to max)

I think the problem is in the first expression of the for cycle, in detail

a*seed

in the second step of the cycle. Any suggestion to solve the problem without splitting

`a*seed`

in

`(100*seed+100*seed+100*seed+...+(a-100*n)*seed`

? :)

Thank you!




Aucun commentaire:

Enregistrer un commentaire