#How to use random?
1 messages · Page 1 of 1 (latest)
I don't recall unity ever recommending random per entity, if you have a source of be interested in reading
As for global random, doesn't really work as it's not thread safe
The youtube channel "turbo makes games" recommends it in one of his videos
So a YouTuber suggests it
Allthough I have no idea if thats a valid source, he mostly just reads out the unity tutorials in a video format
Anyway you could have a global seed to generate per thread randoms in systems
Per entity random has some advantages though in that it's deterministic etc
But it also creates a giant dependency mess in your code
Which I am pretty against
How is per entity more deterministic than per-system?
I can just have one per system, I seed new ones per job and that should be deterministic or not
ah it isn't because the order is not deterministic?
It's more that you need per thread not per system
If you seed a job with a single random every thread will produce the same values
ah yeah ofc
A simple way around this is something like Random.FromIndex(seed + entity.index)
Or better yet using chunk index in a ijc
so I just pass the seed to the job?, and then seed a new random not even in every thread but for every entity?
I've seen half a dozen ways of doing this
None yet that have clearly been the best to me
why can't one just pass in an array of rngs, one per thread?
is the number of threads not deterministic?
that would basically be how you do it in the machine learning frameworks I know
is there a way to obtain thread id in a job?
then I guess you can just write wrapper in native reference
Or something like that
and pass to jobs when you need it
why is thread based random not deterministic? Since the assignment "thread <-> entities it operates on" is not deterministic?
Yeah
but honestly, that sounds like a slow one
Repeat it multiple times and your chunks won't end up on same threads
I think thread random is fastest approach actually
You avoid need to hash seed per entity
but then you have to obtain random value on each entity execute
ok, so per-entity randomness actually is the only way to make it determinstic then, right? Or the thing you said with seeding with entity-id
otherwise due to the non-deterministic thread-id working on your entities you have no chance?
I don't know what you mean
You do it once per chunk
Per chunk is deterministic
yeah, but how would you do it with IJE?
that sounds like a bigger hassle then per-entity though
Ijobentitybeginend
Been in for months!
Thank you for the help!
For random generation I would highly recommend the approach in the patios framework.
It works really well even if you need parallel execution.
And you can extract the Rng toolkit separately of the framework (I think you only need this file: https://github.com/Dreaming381/Latios-Framework/blob/d9b7d2de1afc8d91de59014bed6578e3b146fdc5/Core/Math/Rng.cs)
It does sort of hint in the random source that it was designed to be used on components, whether that’s per entity or per chunk or outdated I dunno 🙃
‘Designed for minimal state (32bits) to be easily embeddable into components.’
Thank you! This looks exactly like what I envisioned and also deterministic - just feels better than one random per entity