How can I best generate a static array of random number on demand?

后端 未结 8 795
北海茫月
北海茫月 2021-01-14 06:56

An application I\'m working on requires a matrix of random numbers. The matrix can grow in any direction at any time, and isn\'t always full. (I\'ll probably end up re-imple

8条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-14 07:19

    A pseudo-random number generator is essentially a function that deterministically calculates a successor for a given value.

    You could invent a simple algorithm that calculates a value from its neighbours. If a neighbour doesn't have a value yet, calculate its value from its respective neighbours first.

    Something like this:

    • value(0,0) = seed
    • value(x+1,0) = successor(value(x,0))
    • value(x,y+1) = successor(value(x,y))

    Example with successor(n) = n+1 to calculate value(2,4):

     \ x  0      1      2
    y  +-------------------
     0 | 627    628    629
     1 |               630
     2 |               631
     3 |               632
     4 |               633
    

    This example algorithm is obviously not very good, but you get the idea.

提交回复
热议问题