How to understand autocorrelations caused by seeding a RNG too much?

后端 未结 2 1503
粉色の甜心
粉色の甜心 2020-12-20 20:55

In response to this question I ran the following VBA experiment:

Sub Test()
    Dim i As Long, A As Variant
    Dim count1 As Long, count2 As Long
    ReDim A         


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-20 21:12

    Partial answer only, fell free to edit and complete.


    Well, there is clearly a correlation when you overuse the Randomize function.

    I tried the following code, with a conditional formatting (black fill for values >0.5), and there is clearly patterns appearing (try to comment the Randomize to see a more "random" pattern. (best seen with 20 pt columns and 10% zoom)

    Function Rndmap()
        Dim i As Long, j As Long
        Dim bmp(1 To 512, 1 To 512) As Long
        For i = 1 To 512
            For j = 1 To 512
                ' Rnd -1 ' uncomment this line to get a big white and black lines pattern.
                Randomize 'comment this line to have a random pattern
                bmp(i, j) = IIf(Rnd() < 0.5, 0, 1)
            Next j
        Next i
        Range(Cells(1, 1), Cells(512, 512)) = bmp
    End Function
    

    So while the MSDN states that "Using Randomize with the same value for number does not repeat the previous sequence.", implying that if the Timer returns twice the same value, the Rnd should keep on the same random sequence without reseting, there is still some behind the scene link..

    Some screenshots:

    Rnd() only: Rnd

    Using Randomize: randomize

    Using Rnd -1 and Randomize: Rnd -1

提交回复
热议问题