Multiply With Carry

This algorithm generates a random number.



									Private Shared Q As UInteger() = New UInteger(4095) {}
Private Shared c As UInteger = 362436
Private Shared i As UInteger = 4095

Public Shared Function MultiplyWithCarry() As UInteger
	Dim t As ULong, a As ULong = 18782
	Dim x As ULong, r As UInteger = &HFFFFFFFEUI

	i = (i + 1) And 4095
	t = a * Q(i) + c
	c = CUInt(t >> 32)
	x = ((t And UInteger.MaxValue) + c) And UInteger.MaxValue

	If x < c Then
		x += 1
		c += 1
	End If

	Q(i) = r - CUInt(x)
	Return Q(i)
End Function
								


Example

									Dim value = MultiplyWithCarry()
								


Output

									4294604858