Analysis of Random Generator
In this post, I explored the randomness of pseudo-random sequences generated by different functions: sinusoidal, power, and linear. By examining the histogram and autocorrelation of these sequences using MATLAB, I demonstrated how sinusoidal and power functions exhibit more randomness than a simple linear function. The autocorrelation plots revealed that the sinusoidal sequence, though not truly random, most closely mimics randomness. This analysis highlights the differences between pseudo-random sequences and true randomness, which is important in systems like GLSL.
Pseudo-random Generator in GLSL
As mentioned in here1, unfortunately, GLSL does not provide a “true” random function. Instead, we can generate a pseudo random number with a sinusoidal function of high amplitude with fract
.
float random(in float x) {
return fract(sin(x)*100000.0);
}
fract
transforms the value into the range [0, 1], and the sinusoidal function provides randomness via varying slopes. Similarly, fract(x^2)
can generate random number since their slopes are different for each piece. However, unlike the sinusoidal function, which is bounded on [-1, 1], x^2
can exceed the maximum value of Float, potentially outputting incorrect values depending on the system.
Using MATLAB, I’ve generated the histogram of the above random variable.
N = 5000;
x = linspace(0, pi, N);
% z = sin(x)*100000;
z = x.^2*100000;
% z = x*100000;
X = z - floor(z);
% X = rand(size(x));
figure();
set(gcf, 'Position', [680 557 720 480])
subplot(211);
histogram(X, 'Normalization', 'pdf');
xlim([0, 1]);
grid on
xlabel('Value, X');
ylabel('PDF, f(X)');
set(gcf, 'color', 'w');
subplot(212);
plot(x, X, '.');
xlim([0, pi]);
ylabel('Value, X');
formula | result |
---|---|
\(\rm sin(\it x)\) | ![]() |
\(x^2\) | ![]() |
\(x\) | ![]() |
Above, the histogram of \(\rm sin(\it x \rm)\), \(x^2\), \(x\) are shown. As mentioned before, sinusoidal and power functions show randomness, whereas linear function has a pattern. Please notice that they have a uniform distribution.
Autocorrelation
Additionally, comparing the autocorrelation of sequences is a good way to evaluate their randomness. The autocorrelation for a discrete sequence \(x(n)\) is given by:
\[R_x(\tau) = \lim_{N \rightarrow \infty} \frac{1}{N} \sum_{n=-N}^{N} x(n) x(n+\tau),\]where \(R_x(\tau)\) is the autocorrelation at delay \(\tau\) and \(x(n)\) is the discrete sequence. For normalized autocorrelation, we divide \(R_x(\tau)\) by \(R_x(0)\), ensuring values remain in the range [-1, 1].
R = zeros(1,N);
tau = linspace(floor(-(N-1)/2), floor((N-1)/2), N);
for i = 1:N
k = tau(i);
R(i) = sum((X-0.5) .* circshift((X-0.5), k));
end
R0 = sum((X-0.5) .* (X-0.5));
R = R / R0;
figure();
plot(tau, R);
grid on;
xlabel('Delay, \tau');
ylabel('Normalized autoorrelation');
Using the MATLAB code above, we can plot the normalized autocorrelation for the pseudo-random sequences mentioned earlier.
formula | normalized autocorrelation |
---|---|
rand |
![]() |
\(\rm sin(\it x)\) | ![]() |
\(x^2\) | ![]() |
\(x\) | ![]() |
Theoretically, the autocorrelation of a random sequence is 1 only when \(\tau = 0\). However, the reason why MATLAB’s rand
function exhibits nonzero autocorrelation for \(\tau \neq 0\) is that it is not “true” random and because the autocorrelation at the above code is computed within a finite window. By comparing the autocorrelation of random sequences generated using \(\rm sin(\it x)\), \(x^2\), and \(x\) with that of the rand
function, we can see that the sequence generated using \(\rm sin(\it x)\) exhibits the good randomness.
Conclusion
This study explored the characteristics of pseudo-random sequences used in GLSL. The \(\rm sin(\it x)\) and \(x^2\) functions demonstrated more random characteristic than the linear function, with the autocorrelation analysis showing that the \(\rm sin(\it x)\) sequence most closely approximates true randomness. It would also be interesting to create custom random generators using the above analysis methodologies for further exploration and application.