Generating pseudo-random numbers in compute shaders without using thread index as seed?

Hey guys, I’ve been Googling for hours but I can’t figure out how to go about this. I’ll try summarize what I’m trying to make:

  • Rudimentary particle simulator using a compute shader
  • Buffer of custom Particle structs
  • 1 dimensional thread groups using thread index as buffer index
  • Hash function to generate starting positions

The hash function works great for initiating the starting positions. However, if I want to get for example a random value to use as a velocity, that value will always be tied to the thread index; thus the order of the particle in the buffer will determine the generated value.

I’m very new to compute shaders so this is all super confusing to me, I’m not sure if I explained it right but any help would be appreciated!

Create a variable you can assign to in the compute shader that will hold another value to add to your thread index before you hash it. You can update this value each time you run the compute. Assign something like Time.realtimeSinceStartup to it for example, or use some C# random value.

By each time I run the compute do you mean once per compile time when I run the code in Unity or each update loop?

Each Dispatch() call. If you’re running one every Update() then yeah.

If you’re dispatching multiple times and update and want to avoid the results of 2 different dispatches potentially having the same resulting hashes for their “randomized” values, then you’ll want to include an updated Random value assigned to the Compute before the Dispatch that isn’t simply the current time, so Random.value for example.

1 Like

Got it working, thank you very much!