Reputation:
I have been looking at a lot of questions regarding generating a random number in the fragment shader. However, none of them are of shape:
float rand() {
...
return random_number;
}
All of them require a input parameter. Is there any way to get a random number without any input?
Upvotes: 2
Views: 4421
Reputation: 473407
Computers are deterministic devices that execute algorithms. Randomness cannot manifest itself out of thin air. While many CPUs do have some form of randomness-generating construct, even that takes some kind of input, even if you don't see it.
This is why random number algorithms are technically called "pseudo-random number" generators (PRNG). They take an input and generate an output based on that input. The trick with PRNGs is that they generate output that (theoretically) is not predictable from the input. Outside of hardware-based random generators, all computer RNGs work this way.
Even C/C++'s rand
function does, though it hides the input behind the seed state set by srand
. rand
uses internal state which can be initialized by srand
, and that state is used as the input to the PRNG function. Every execution of rand
changes this state, presumably in a way that is not easy to predict.
You could theoretically try the rand
trick in a shader, by using Image Load/Store functionality to store the internal state. However, that would still qualify as input. The function might not take input, but the shader certainly would; you'd have to provide a texture or buffer to use as the internal state.
Also, it wouldn't work, due to the incoherent nature of image load/store reading and writing. Multiple invocations would be reading from and writing to the same state. That can't happen.
Upvotes: 1