mardi 16 août 2016

Unity: How do I generate multiple different random numbers in the same frame?

I'm attempting to generate a random vector3 by generating the x, y and z components individually (afaik there is no way to generate a random vector3 outright).

I have this part working fine;

I create the seed Random.seed = DateTime.Now.Ticks etc

I then generate the random numbers;

float x = Random.Range(-10000, 10000);
float y = Random.Range(-10000, 10000);
float z = Random.Range(-10000, 10000);

I then have some code that will check to ensure that this vector generated is unique.

The issue lies in that, because this is happening in the same frame, the x, y and z values are allthe same.

For example, if the random generator generates x = -1402.345, then y and z also shall be -1402.345.

I have attempted to put this generation into a coroutine which would solve the issue (by yield return new WaitForEndOfFrame etc) however when I want to then check these values and if need be regenerate them, I cannot without starting a new coroutine . Seeing as these vectors are also being saved to a database, where I want to generate hundreds (possibly thousands) of data entries, starting a new coroutine each time the generated vector is invalid is impractical and will likely just end up breaking my program.

Is there a simpler way to generate 3 random numbers in the same frame? I had considered simply changing the seed, but am unsure if this is good practice or if there is a better way?




Aucun commentaire:

Enregistrer un commentaire