[Numpy-discussion] Another Array

Robert Kern robert.kern@gmail....
Thu Apr 9 23:26:16 CDT 2009

On Thu, Apr 9, 2009 at 23:18, Ian Mallett <geometrian@gmail.com> wrote:
> Hello,
> With the help of this list, over the past two days, I have implemented a GPU
> particle system (picture here:
> http://img7.imageshack.us/img7/7589/image1olu.png).  Every particle is
> updated entirely on the GPU, with texture data (arrays) being updated
> iteratively between two framebuffer objects.  Here, 65,536 particles are
> drawn at ~50 fps.
> Currently, jitter is introduced into the system by means of a "random
> texture", which should store random unit 3D vectors encoded into the RGB
> channels (vectors point in all directions, positive and negative, so the
> vecotr is actually 0.5 long and added to 0.5).  The random texture is
> constructed using Python's random number generation, which is noticeably
> slower.  I also didn't find an efficient way of creating "random" vectors.
> All this contributes to the pattern artifact at the bottom of the
> screenshot.  I'd like to use NumPy.
> The array should be constructed as n*n*3, with every vec3 being a normalized
> random vector with each component in the range [-1.0,1.0].  I could use
> numpy.random.random(), but this would not give the appropriate vectors.
> Every 3D vector should have the same probability of being chosen.  I don't
> know how to do that in NumPy--that's where I need help.

A quick trick, normalizing standard multivariate Gaussians yields
uniformly sampled unit vectors:

  vecs = numpy.random.standard_normal(size=(n,n,3))
  magnitudes = numpy.sqrt((vecs*vecs).sum(axis=-1))
  uvecs = vecs / magnitudes[...,newaxis]
  rgb = (uvecs + 1.0) / 2.0

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco

More information about the Numpy-discussion mailing list