Algorithms for biased spawning of objects

In my game, I would like to create N elements, not necessarily at the same time. Some elements depend on what was created before (Markov chain-esque), so the probability of spawning two rocket launchers in a row is low, but there is a reasonable probability of a rocket launcher with subsequent missiles. What would be the most effective way to do this? The method will be called very often, so I'm trying to minimize the calculation.

The idea I came up with might be to create an N x N array that acts like a probability lookup table (the element previously spawned the VS element to appear). However, in this process I will need some way of generating a random number with a probability acting like an offset. I'm not sure the best way to do this. Things are also a little confused when inventory comes into play, since rockets cannot be generated if the quantity Y is already generated. I could create a 3D array and store the inventory number there, but I'm not sure how efficient it would be to keep updating the array lookup table based on the inventory.

This is just an idea that I came across, but there is probably another, better way to do this. Are there any data structures that would be more efficient than a 3D array, or algorithms that I should read?

+3
source share
2 answers

The most effective way, if you do not need a lot of saved state, is to do what you hinted at: create a Markov chain. Associated with each state is the array of probabilities of reaching the next state. This gives you complete control over the process and is quite compact. (Note that you use it by generating a random number from 0 to 1 and performing a binary search on aggregate probabilities.)

, - . ,

launcher_bias = 0.8*launcher_bias + 0.2*(1.0 - (last_item == launcher))
rocket_bias = 0.8*rocket_bias + 0.2*(last_item == launcher)

( 1 , 0,7 - , 0 0,7), , , . , , . , , , .

+4

, . , ?

, 4

  • A, : 4
  • B, : 1
  • C, : 1
  • D, : 10

16, X [0,16). A, B ..

  • 4 X. X , A.
  • else, 1 X. X , B.
  • else, 1 X. X , C.
  • else, D.
+3

Source: https://habr.com/ru/post/1758982/


All Articles