#How to use random?

1 messages · Page 1 of 1 (latest)

high locust
#

I have read that I should be adding Randoms to each entity that needs any random-ness. To be honest, that does not feel easily controllable to me. I'd rather just have one global random rng, or at least one per system. Is there a reason for Unity choosing the rng-per-entity approach, am I shooting myself in the foot?

scarlet lodge
#

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

high locust
#

The youtube channel "turbo makes games" recommends it in one of his videos

scarlet lodge
#

So a YouTuber suggests it

high locust
#

Allthough I have no idea if thats a valid source, he mostly just reads out the unity tutorials in a video format

scarlet lodge
#

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

high locust
#

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?

scarlet lodge
#

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

high locust
#

ah yeah ofc

scarlet lodge
#

A simple way around this is something like Random.FromIndex(seed + entity.index)

#

Or better yet using chunk index in a ijc

high locust
#

so I just pass the seed to the job?, and then seed a new random not even in every thread but for every entity?

scarlet lodge
#

I've seen half a dozen ways of doing this

#

None yet that have clearly been the best to me

high locust
#

why can't one just pass in an array of rngs, one per thread?

#

is the number of threads not deterministic?

scarlet lodge
#

Totally doable

#

That's a way I've seen done as well

high locust
#

that would basically be how you do it in the machine learning frameworks I know

scarlet lodge
#

Thread based random is not deterministic though

#

But most games don't care

grim oxide
#

is there a way to obtain thread id in a job?

scarlet lodge
#

Yes

#

[NativeSetThreadIndex]

grim oxide
#

then I guess you can just write wrapper in native reference

scarlet lodge
#

Or something like that

grim oxide
#

and pass to jobs when you need it

high locust
#

why is thread based random not deterministic? Since the assignment "thread <-> entities it operates on" is not deterministic?

scarlet lodge
#

Yeah

grim oxide
#

but honestly, that sounds like a slow one

scarlet lodge
#

Repeat it multiple times and your chunks won't end up on same threads

scarlet lodge
#

You avoid need to hash seed per entity

grim oxide
high locust
#

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?

grim oxide
#

which would be double random access

#

first to thread map, then to random itself

scarlet lodge
#

You do it once per chunk

grim oxide
#

yeah, but how would you do it with IJE?

high locust
scarlet lodge
grim oxide
#

wait what?

#

they implemented that?

#

damn

scarlet lodge
#

Yeah Dani did it for me in pre 15

#

Best thing ever added

grim oxide
#

all right all right

#

and here I was dreaming about it just yesterday

#

😅

scarlet lodge
#

Been in for months!

grim oxide
#

I haven't really used entities

#

in more than a month

#

stuck with webgl quest game

high locust
#

Thank you for the help!

vague marten
remote condor
high locust