Seed Based Matrix Randomization

So today i found myself in the odd circumstance that i needed a random value when given an X and Y coordinate naturally doing a dual for loop for each resulted in a horrible slow method that completely destroyed performance.

So i came up with the idea of caching randomized values in a 2D matrix and pulling them our as required, naturally this has the problem what if your looking for in X and Y Coordinates and your cache is not large enough to support those values? the solution Modulus looping and thus i present to you Matrix Randomization. please find the open source matrix randomization as part of the rm2kdev.toolkit namespace

Basic usage is simply initializing it with a length of 100 and whatever seed you like if you’d like a larger scrolling offset increase the length but know a larger array will result in longer look up times and more memory usage but for my uses I’ve found 100 wide to work perfectly.

'Copyright rm2kdev, free for use by anyone.
Namespace Rm2kdev.Toolkit
    Public Class MatrixRandom

        Private Randomizer As System.Random = New System.Random()
        Private Length As Integer
        Private Matrix(,) As Double

        Public Sub New(Length As Integer, Seed As Integer)
            Me.Length = Length
            ReDim Matrix(Length, Length)
            Randomizer = New System.Random(Seed)
            For i = 0 To Length
                For j = 0 To Length
                    Matrix(i, j) = Randomizer.NextDouble()
        End Sub

        Public Function RandomXY(X As Integer, Y As Integer) As Double
            Return Matrix(X Mod Length, Y Mod Length)
        End Function

    End Class
End Namespace
Liked it? Take a second to support rm2kdev on Patreon!