我怎样才能最好的产生的随机数需求静态数组? [英] How can I best generate a static array of random number on demand?

查看:127
本文介绍了我怎样才能最好的产生的随机数需求静态数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个应用程序需要随机数的矩阵。基质可以在任何方向在任何时间增长,而且并不总是充分。 (用四叉树或别的东西,我可能会最终重新实现它,而不是有很多空对象的矩阵。)

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-implementing it with a quad tree or something else, rather than a matrix with a lot of null objects.)

我需要一种方法来生成相同的矩阵,给予相同的种子,无论以何种顺序我计算矩阵。

I need a way to generate the same matrix, given the same seed, no matter in which order I calculate the matrix.

LazyRandomMatrix rndMtx1 = new LazyRandomMatrix(1234) // Seed new object
float X = rndMtx1[0,0] // Lazily generate random numbers on demand
float Y = rndMtx1[3,16]
float Z = rndMtx1[23,-5]

Debug.Assert(X == rndMtx1[0,0])
Debug.Assert(Y == rndMtx1[3,16])
Debug.Assert(Z == rndMtx1[23,-5])

LazyRandomMatrix rndMtx2 = new LazyRandomMatrix(1234) // Seed second object
Debug.Assert(Y == rndMtx2[3,16])  // Lazily generate the same random numbers
Debug.Assert(Z == rndMtx2[23,-5]) // on demand in a different order
Debug.Assert(X == rndMtx2[0,0])

是的,如果我知道数组的大小,最好的办法是产生整个阵列,只是返回值,但他们需要独立和需求产生的。

Yes, if I knew the dimensions of the array, the best way would be to generate the entire array, and just return values, but they need to be generated independently and on demand.

我的第一个想法是初始化为每个调用一个新的随机数生成到一个新的坐标,与整个矩阵的种子一些散在调用中使用的坐标播种,但是这似乎是一个可怕的黑客,因为它会需要创造大量的新随机的对象。

My first idea was to initialize a new random number generator for each call to a new coordinate, seeding it with some hash of the overall matrix's seed and the coordinates used in calling, but this seems like a terrible hack, as it would require creating a ton of new Random objects.

推荐答案

什么你说的是通常被称为柏林噪声,这里有一个链接,你:的 http://freespace.virgin.net/hugo.elias/models/m_perlin.htm

What you're talking about is typically called "Perlin Noise", here's a link for you: http://freespace.virgin.net/hugo.elias/models/m_perlin.htm

在那篇文章中,最重要的是2D的噪声功能:

The most important thing in that article is the noise function in 2D:

  function Noise1(integer x, integer y)
    n = x + y * 57
    n = (n<<13) ^ n;
    return ( 1.0 - ( (n * (n * n * 15731 + 789221) + 1376312589) & 7fffffff) / 1073741824.0);    
  end function

它返回一个数字,-1.0和+1.0之间仅基于x和y coordonates(和硬codeD的种子,你可以在你的应用程序开始随意改变或只是离开它,因为它是)。

It returns a number between -1.0 and +1.0 based on the x and y coordonates alone (and a hard coded seed that you can change randomly at the start of your app or just leave it as it is).

在本文的其余部分是关于如何随机希望这些数字内插这些数字,但根据,你可以离开他们,因为它是。需要注意的是,这些数字将会完全随机的。如果改为应用余弦插补,并使用所产生的噪音每隔5-6指标​​,插其间,你会得到高度图数据(这是我用了)。跳过它完全随机的数据。

The rest of the article is about interpolating these numbers, but depending on how random you want these numbers, you can just leave them as it is. Note that these numbers will be utterly random. If you instead apply a Cosine Interpolator and use the generated noise every 5-6 indexes, interpolating inbetween, you get heightmap data (which is what I used it for). Skip it for totally random data.

这篇关于我怎样才能最好的产生的随机数需求静态数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆