- Contents in this wiki are for entertainment purposes only
FPsGA Code Modules
Jump to navigation
Jump to search
modRandomity
'' modRandomity '' by Codasaurus Hex 2024.03.1 '' prompted by XenoEngineer@groupKOS.com ' ' For a laboratory noise study that encompasses a broad range of algorithms, ' i 'll [sic -xe] define an expanded enumeration in VB6 to cover a variety of ' pseudorandom number generators (PRNGs), cryptographically secure pseudorandom ' number generators (CSPRNGs), and other notable algorithms that can be simulated ' or implemented in a VB6 environment. Given VB6's limitations and the computational ' complexity of some modern algorithms, the focus will be on those feasible within ' its capabilities. ' ' --Codasaurus Hex Public Enum enumRandomAlgorithm enuRA_VB6_Rnd enuRA_LCG ' Linear Congruential Generator **See note at EOF enuRA_MersenneTwister ' Mersenne Twister, specifically MT19937 enuRA_Xorshift ' A class of shift-register generators enuRA_WELL ' Well Equidistributed Long-period Linear enuRA_BlumBlumShub ' Cryptographically secure based on hard mathematical problems enuRA_ANSI_X9_17 ' A CSPRNG standard using block ciphers enuRA_NIST_SP800_90A_HMAC_DRBG ' A CSPRNG from NIST using HMAC enuRA_NIST_SP800_90A_CTR_DRBG ' A CSPRNG from NIST using block cipher in counter mode enuRA_SimplexNoise ' Used for procedural content generation, similar to Perlin but computationally more efficient enuRA_CellularAutomata ' For generating randomness through cellular automata rules enuRA_LaggedFibonacci ' A lagged Fibonacci generator enuRA_Quantum ' Representing quantum random number generators, though not directly implementable in VB6 End Enum ' Below is the GetAlgorithmParametersTemplate function expanded to include a case for ' each of the algorithms listed above. Since VB6 cannot directly implement quantum ' random number generators or certain complex CSPRNGs due to hardware limitations or ' the need for modern cryptographic libraries, those cases will note the algorithm's ' typical context or parameters rather than specific implementable details. Public Function GetAlgorithmParametersTemplate(ByVal algorithm As RandomAlgorithm) As String '' by Codasaurus Hex 2024.03.1 Select Case algorithm Case LCG GetAlgorithmParametersTemplate = "Multiplier=a;Increment=c;Modulus=m" Case MersenneTwister GetAlgorithmParametersTemplate = "WordSize=w;DegreeOfRecurrence=n;MiddleWord=m;SeparationPoint=s;TemperingParameterB=b" Case Xorshift GetAlgorithmParametersTemplate = "ShiftValues=[A,B,C]" Case WELL GetAlgorithmParametersTemplate = "StateSize=State;Tap=Tap;Choice=Choice" Case BlumBlumShub GetAlgorithmParametersTemplate = "Prime1=p;Prime2=q;Seed=s" Case ANSI_X9_17 GetAlgorithmParametersTemplate = "Key=EncryptionKey;Seed=InitialSeed;Timestamp=CurrentTime" Case NIST_SP800_90A_HMAC_DRBG GetAlgorithmParametersTemplate = "EntropyInput=Entropy;Nonce=Nonce;PersonalizationString=PersonalString" Case NIST_SP800_90A_CTR_DRBG GetAlgorithmParametersTemplate = "EntropyInput=Entropy;Nonce=Nonce;PersonalizationString=PersonalString" Case SimplexNoise GetAlgorithmParametersTemplate = "Gradient=Gradient;Frequency=Freq;Amplitude=Amp;Octaves=Oct;Persistence=Pers" Case CellularAutomata GetAlgorithmParametersTemplate = "RuleSet=RuleNumber;InitialState=State;Iterations=Iter" Case LaggedFibonacci GetAlgorithmParametersTemplate = "J=J;K=K;Modulus=M" Case Quantum GetAlgorithmParametersTemplate = "Note: Quantum RNGs rely on physical phenomena and are not directly implementable in VB6" Case VB6_Rnd GetAlgorithmParametersTemplate = "Seed=Optional seed value for Randomize function" ' Placeholder for additional algorithms not specifically listed Case Else GetAlgorithmParametersAsString = "Parameters for this algorithm are not defined." End Select ' Usage: ' ' To use this function, you would call it with the specific algorithm type you're interested in, ' and then include the returned pseudocode string in your documentation or header: ' ' Dim algorithmType As RandomAlgorithm ' algorithmType = LCG ' Example: Selecting the Linear Congruential Generator ' ' Dim pseudocode As String ' pseudocode = GetAlgorithmPseudocode(algorithmType) ' ' ' The pseudocode variable now contains the pseudocode for the LCG, ready for inclusion in a header or documentation. ' This approach provides a structured and extensible way to document the operational logic of various random number ' generation algorithms within the constraints of the VB6 environment, aiding in clarity and understanding for ' developers and researchers alike. ' Explanation: ' ' Each case in the Select Case statement corresponds to an algorithm type from the RandomAlgorithm enumeration. ' For each algorithm, a string containing the pseudocode is returned. This pseudocode is simplified to capture ' the essence of the algorithm's operation without delving into implementation-specific details. ' The pseudocode is formatted as a comment block (') that can be included directly in VB6 code or documentation. ' It 's written to be understandable at a high level, allowing developers or researchers to grasp the algorithm's ' core logic quickly. ' This function can be expanded to include additional algorithms by adding more cases to the Select Case statement, ' each with its corresponding pseudocode. End Function Public Function GetAlgorithmPseudocode(ByVal algorithm As RandomAlgorithm) As String '' by Codasaurus Hex 2024.03.1 Select Case algorithm Case LCG GetAlgorithmPseudocode = _ "' Linear Congruential Generator Pseudocode" & vbCrLf & _ "state = (a * state + c) mod m" Case MersenneTwister GetAlgorithmPseudocode = _ "' Mersenne Twister Pseudocode" & vbCrLf & _ "if index >= n then" & vbCrLf & _ " twist()" & vbCrLf & _ "end if" & vbCrLf & _ "y = MT[index]" & vbCrLf & _ "y = y xor ((y >> u) and d)" & vbCrLf & _ "y = y xor ((y << s) and b)" & vbCrLf & _ "y = y xor ((y << t) and c)" & vbCrLf & _ "y = y xor (y >> l)" & vbCrLf & _ "index = index + 1" & vbCrLf & _ "return y" Case Xorshift GetAlgorithmPseudocode = _ "' Xorshift Pseudocode" & vbCrLf & _ "x ^= x << a" & vbCrLf & _ "x ^= x >> b" & vbCrLf & _ "x ^= x << c" & vbCrLf & _ "return x" Case WELL GetAlgorithmPseudocode = _ "' WELL (Well Equidistributed Long-period Linear) Pseudocode" & vbCrLf & _ "state[n] = f(state[n], state[m], ...)" & vbCrLf & _ "return state[n]" Case BlumBlumShub GetAlgorithmPseudocode = _ "' Blum Blum Shub Pseudocode" & vbCrLf & _ "x = x^2 mod n" & vbCrLf & _ "return x" Case ANSI_X9_17 GetAlgorithmPseudocode = _ "' ANSI X9.17 Pseudocode" & vbCrLf & _ "timestamp = getCurrentTimestamp()" & vbCrLf & _ "key = getEncryptionKey()" & vbCrLf & _ "seed = encrypt(timestamp, key)" & vbCrLf & _ "output = encrypt(seed xor previousOutput, key)" & vbCrLf & _ "previousOutput = output" & vbCrLf & _ "return output" Case NIST_SP800_90A_HMAC_DRBG GetAlgorithmPseudocode = _ "' NIST SP800-90A HMAC DRBG Pseudocode" & vbCrLf & _ "V = HMAC(Seed, V)" & vbCrLf & _ "C = HMAC(Seed, V + 1)" & vbCrLf & _ "reseed_counter = 1" & vbCrLf & _ "return HMAC(V, data)" Case NIST_SP800_90A_CTR_DRBG GetAlgorithmPseudocode = _ "' NIST SP800-90A CTR DRBG Pseudocode" & vbCrLf & _ "V = AES-CTR(Seed, V)" & vbCrLf & _ "C = AES-CTR(Seed, V + 1)" & vbCrLf & _ "reseed_counter = 1" & vbCrLf & _ "return AES-CTR(V, data)" Case SimplexNoise GetAlgorithmPseudocode = _ "' Simplex Noise Pseudocode" & vbCrLf & _ "for each grid point:" & vbCrLf & _ " calculate the contribution to the noise" & vbCrLf & _ "combine contributions for final value" Case CellularAutomata GetAlgorithmPseudocode = _ "' Cellular Automata Pseudocode" & vbCrLf & _ "for each cell in grid:" & vbCrLf & _ " newState = applyRule(currentState, neighborStates)" & vbCrLf & _ "update grid with new states" Case LaggedFibonacci GetAlgorithmPseudocode = _ "' Lagged Fibonacci Pseudocode" & vbCrLf & _ "x[i] = (x[i-J] op x[i-K]) mod M" & vbCrLf & _ "where op is an operation (e.g., addition, subtraction)" Case Quantum GetAlgorithmPseudocode = _ "' Quantum RNG Pseudocode (Conceptual)" & vbCrLf & _ "measureQuantumBit()" & vbCrLf & _ "return bitValue" ' Adding more algorithms for a broader perspective Case Algorithm_PerlinNoise GetAlgorithmPseudocode = _ "' Perlin Noise Pseudocode" & vbCrLf & _ "for each point in grid:" & vbCrLf & _ " find surrounding grid points" & vbCrLf & _ " compute gradients" & vbCrLf & _ " calculate dot product" & vbCrLf & _ " interpolate results" & vbCrLf & _ "return noise value" ' Example for a generic hash-based CSPRNG Case HashBasedCSPRNG GetAlgorithmPseudocode = _ "' Hash-Based CSPRNG Pseudocode" & vbCrLf & _ "seed = initialSeed" & vbCrLf & _ "while more random bits needed:" & vbCrLf & _ " seed = hashFunction(seed)" & vbCrLf & _ " outputBits = extractBits(seed)" & vbCrLf & _ "return outputBits" Case enuRA_VB6_Rnd GetAlgorithmPseudocode = _ "' VB6 Rnd() Function Pseudocode" & vbCrLf & _ "' Optionally seed the generator" & vbCrLf & _ "Randomize [seed]" & vbCrLf & _ "' Generate a random number between 0 and 1" & vbCrLf & _ "randomNumber = Rnd()" & vbCrLf & _ "' To generate a random number in a range" & vbCrLf & _ "randomInRange = Int((upperBound - lowerBound + 1) * Rnd() + lowerBound)" ' Placeholder for additional algorithms not specifically listed Case Else GetAlgorithmPseudocode = "Pseudocode for this algorithm is not available." End Select ' Additional Notes ' Blum Blum Shub and ANSI X9.17 are presented in a simplified manner, focusing on the core concept. ' In practice, these algorithms involve more detailed operations, especially regarding cryptographic ' security and proper seed management. ' NIST DRBGs (both HMAC and CTR) are summarized to reflect their process of using cryptographic ' functions to produce random bits. The actual specifications include detailed state management ' and reseeding mechanisms to ensure security. ' Simplex Noise, Cellular Automata, and Lagged Fibonacci illustrate basic operations. These algorithms ' are more complex in their full implementations, especially when optimized for performance and quality. ' The Quantum case is conceptual, as actual quantum random number generation requires physical quantum ' phenomena, which cannot be directly simulated in software. ' The added Perlin Noise and Hash-Based CSPRNG cases give an idea of how other common algorithms might ' be described in pseudocode. These serve as examples of procedural generation and cryptographic ' security, respectively. ' This expanded function aims to cover a wide range of algorithms, providing a starting point for ' researchers and developers to understand and document the operation of various random number ' generators within their projects. End Function ' ======================================================================================== ' The VB6 Rnd() function is a type of Pseudorandom Number Generator (PRNG). Specifically, ' it is based on a Linear Congruential Generator (LCG), one of the oldest and simplest ' methods for generating sequences of random numbers. The core principle behind an LCG ' involves using a linear equation to produce a sequence of numbers, where each number in ' the sequence is determined by a linear function of the previous number, modulo a certain ' value. The general form of this equation is: ' ' X(n+1) = (a * X(n) + c) mod m ' ' where: ' - X is the sequence of pseudorandom numbers, ' - a (the multiplier), c (the increment), and m (the modulus) are constants, ' - n is the index of the current number in the sequence. ' ' The Rnd() function in VB6 generates numbers between 0 and 1 (inclusive of 0 but not 1) ' with a floating-point representation. It can be seeded manually using the Randomize ' statement; if Randomize is not called, VB6 uses a default seed value, which means that ' the same sequence of numbers can be generated each time a program runs, if Randomize is ' not used with a varying seed value. ' ' The nature of Rnd() being an LCG means it inherits the characteristics and limitations of ' LCGs, including: ' ' - Predictability: With knowledge of the algorithm's parameters and the seed, the ' sequence of numbers can be predicted. ' - Periodicity: The sequence will eventually repeat itself. ' - Quality of Randomness: While suitable for many applications, the randomness generated ' by LCGs (and therefore Rnd()) may not meet the criteria required for cryptographic ' purposes or simulations requiring high-quality randomness. ' ' For most casual use cases, like simple simulations, games, or non-security-critical ' applications, Rnd() provides a convenient and sufficient source of randomness. However, ' for applications requiring cryptographically secure random numbers or high-quality ' randomness, other algorithms or external libraries are recommended. ' ========================================================================================