A
A
Almost2021-08-10 10:31:33
Unity
Almost, 2021-08-10 10:31:33

How to make different random generators?

Let's say at the beginning of the game each time a map is generated.
Generation depends on randomness.
To make the generation repeatable, the same seed is used at the beginning of the generation.

For example, after generation, entity damage is dealt, depending on randomness, and this damage will be the same every time, because. seed was used before generation.

Example:

spoiler
Random.InitState(10);

for (int i = 0; i < 3; i++)
{
  Debug.Log($"GENERATION : {Random.Range(0f, 1f)}");
}
      
for (int i = 0; i < 3; i++)
{
  Debug.Log($"SHOOT : {Random.Range(0f, 1f)}");
}


Вывод каждый раз одинаковый:
GENERATION : 0.2686557
GENERATION : 0.7817978
GENERATION : 0.9014543

SHOOT : 0.3051965
SHOOT : 0.8728517
SHOOT : 0.2956414

How to make sure that this damage does not depend on the seed that was set at the beginning of the generation?

A solution was found in which before the generation (or before the first installation of the seed) we generate a seed for damage, which will be set before the damage is dealt and updated with a new one after the damage so that the damage does not repeat, but this solution does not seem to be correct and systemic .

Example:
spoiler
var shootSeed = Random.Range(int.MinValue, int.MaxValue);
Random.InitState(10);

for (int i = 0; i < 3; i++)
{
  Debug.Log($"GENERATION : {Random.Range(0f, 1f)}");
}
      
for (int i = 0; i < 3; i++)
{
  Random.InitState(shootSeed);
  Debug.Log($"SHOOT : {Random.Range(0f, 1f)}");
  shootSeed = Random.Range(int.MinValue, int.MaxValue);
}


Выводы:

1)
GENERATION : 0.2686557
GENERATION : 0.7817978
GENERATION : 0.9014543

SHOT : 0.4784655
SHOT : 0.2666625
SHOT : 0.5826535

2)
GENERATION : 0.2686557
GENERATION : 0.7817978
GENERATION : 0.9014543

SHOT : 0.7529675
SHOT : 0.8416272
SHOT : 0.09183574

и т.д.


I seem to be moving in the right direction, but I don't fully understand.
The question still remains how to continue the random call, depending on the seed for generation after shooting. Remember the number of random calls to generate, generate garbage randoms and then get the desired value? It seems to be an overhead.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2021-08-10
@Almost

Use not a unit, but a sharp random - from the System space. He knows how to instance with different seed

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question