We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Gaussian Random Numbers
Page Index Toggle Pages: 1
Gaussian Random Numbers (Read 3273 times)
Gaussian Random Numbers
Mar 18th, 2009, 10:29pm
 
Hi,

I'd like to create a gaussian random number distribution. I'm using the java Random and nextGaussian() combo to do this. Unfortunately I have no control over either the standard deviation used nor over the bounds of the numbers returned - with max/min values varying by a third or more from one run to the next. I'm getting around the bounds issue by coding bounds, testing to see if those bounds are violated and if they are re-generating the random number. It would be nice to not have to do this and to be able to alter the value of the standard deviation.  

Does anyone know of an alternative means of generating a gaussian-like distribution? Thanks.
Re: Gaussian Random Numbers
Reply #1 - Mar 19th, 2009, 10:59am
 
Can't you control the bounds by simple multiplication? (Perhaps I don't fully understand the problem.)

For example I limit them to visualize the distribution:
Code:
int RANGE = 500;

void setup()
{
 size(500, 300);
 background(255);
 
 int[] vals = new int[RANGE];
 
 Random rnd = new Random();
 double mx = 0, mn = 1000;
 for (int i = 0; i < 30000; i++)
 {
   double v = rnd.nextGaussian();
   if (v > mx) mx = v;
   if (v < mn) mn = v;
   // Found that values seem to go from -4 to 4
   // with occasional abs values beyond (below 4.5)
   double val = (v + 5) / 10;
   int idx = (int)(val * RANGE);
   if (idx < 0 || idx > RANGE)
   {
     // Rare but happens (more when I use 4 as limit)
     println("Oops: " + v);
     continue;
   }
   vals[idx]++;
 }
 println("MX " + mx + " MN " + mn);
 stroke(#000088);
 for (int i = 0; i < RANGE; i++)
 {
   line(i, height, i, height - vals[i]);
 }
}

Not sure how to control standard deviation, though, I am not mathematician.
Re: Gaussian Random Numbers
Reply #2 - Mar 19th, 2009, 2:55pm
 
Hi PhiLho,

Thanks for the as always excellent reply. As to the bounds, I'm just picking nits. With the standard random() I can specify the upper and lower limits when I invoke the function. Unfortunately (as far as I can tell) I have no such control when using the Java Random() - nextGaussian() combo. I therefore have to write my own additional code to jigger the returned values. I was hoping that there was an alternative to the Java gaussian implementation that would provide me with the versatility I'm seeking.  

As to dealing with the standard deviation, that will control how the values are dispersed. A smaller stddev results in a more tightly packed distribution while a larger std dev produces a wider, flatter distribution. Having the ability to control the value of the standard deviation would be really useful.  

Unfortunately I'm no mathematician either but am fascinated by algorithmic art. Right now I'm reading "The Computational Beauty of Nature" by Gary Flake trying to understand the math and converting its C programs to run within Processing - which has absolutely nothing to do with my question here.

Thanks again for your help.


Re: Gaussian Random Numbers
Reply #3 - Mar 19th, 2009, 3:43pm
 
You're unlikely to find a Gaussian function that has bounds, gaussian numbers by definition range from -inf to +inf.. they're just highly highly unlikely to range outside of a few standard deviations from 0.
Re: Gaussian Random Numbers
Reply #4 - Mar 19th, 2009, 3:51pm
 
I understood the standard deviation, changing would change my curve, making its side steeper or not. I just don't know how to compute it.
Have you looked at the reference of nextGaussian They give the (simplified) code they use to generate the Gaussian numbers. Perhaps with that and a little googling, you can tweak it to your needs. Smiley
Re: Gaussian Random Numbers
Reply #5 - Mar 19th, 2009, 5:04pm
 
I had this exact same need a while back, so I coded up a gaussian random number generator.  And hey, my first excuse to post something on OpenProcessing.org!

http://openprocessing.org/visuals/?visualID=1254

Enjoy!
Re: Gaussian Random Numbers
Reply #6 - Mar 19th, 2009, 5:17pm
 
Hi PhiLho,
PhiLho  wrote on Mar 19th, 2009, 3:51pm:
Have you looked at the reference of nextGaussian They give the (simplified) code they use to generate the Gaussian numbers. Perhaps with that and a little googling, you can tweak it to your needs. Smiley


Found that and did the google. Of the pages I checked, the Java reference page was the best. I've got it coded up and am trying to understand the impact of the different parms in the equation. This is way more time than I wanted to spend on this but it if works out, at the end I'll have something usable across multiple programs.


Re: Gaussian Random Numbers
Reply #7 - Mar 19th, 2009, 5:19pm
 
P.S.  The sketch has some other useful stuff in it, for calculating explicit gaussian probabilities and standard deviations, so that the sketch can prove the validity of the underlying gaussian random number generator, but if all you want is the latter it's pretty short:

Quote:
// return a random value in the range [0..1] with normal distribution around 0.
// Implements the Marsaglia Polar Method, as described in wikipedia, but only re
turns one of the values.

float randomNormal()
{
  float x = 1.0, y = 1.0,
        s = 2.0; // s = x^2 + y^2
  while(s >= 1.0)
  {
    x = random(-1.0f, 1.0f);
    y = random(-1.0f, 1.0f);
    s = x*x + y*y;
  }
  return x * sqrt(-2.0f * log(s)/s);
}



See the sketch on OpenProcessing if you're not clear how to use it in practice.
Re: Gaussian Random Numbers
Reply #8 - Mar 19th, 2009, 5:22pm
 
Hi Cloister,
cloister wrote on Mar 19th, 2009, 5:04pm:
I had this exact same need a while back, so I coded up a gaussian random number generator.  And hey, my first excuse to post something on OpenProcessing.org!

http://openprocessing.org/visuals/?visualID=1254

Enjoy!


Thanks for the link. I've grabbed your code. Hopefully between what you've accomplished with your program and what I'm trying to figure out with my program I'll get the hang of it.  


Re: Gaussian Random Numbers
Reply #9 - Mar 20th, 2009, 6:32pm
 
The Random.nextGaussian() method that PhiLo linked to uses the same algorithm as my code, but may well be a better option.  In particular, the Marsaglia Polar Method generates _pairs_ of gaussian random numbers.  My code returns one and throws away the other.  Their code caches the other one and returns it if it's available.  So, if you need to generate billions and billions of gaussian random numbers, the Random.nextGaussian() method should be about 2x faster.  Also, it's thread-safe if that matters in your code.

Don't forget that you'll need a "import java.util.Random;" at the top of your Processing file, though.

Silly me for not knowing enough at the time I wrote mine to understand that Processing would have access to all of Java's core classes!  Still, it was a good learning experience to have written it.
Re: Gaussian Random Numbers
Reply #10 - Mar 21st, 2009, 2:27pm
 
Hi Cloister,

Yes I had been using the Java Random and nextGaussian() in a couple prior programs but want more control than that option supports.  And I didn't want to be creating unique code every time I wanted to use nextGaussian() which is why I decided to seek out a replacement.  I haven't had a chance to play with the code since my last post here. Not sure if I'll have time today. If I do come up with a solution, I'll post the code.

Thanks for your help.
Re: Gaussian Random Numbers
Reply #11 - Oct 3rd, 2009, 9:52pm
 
If you want to change the standard deviation, just multiply the output by whatever standard deviation you want. If you want the std to be 10, just multiply the output by 10.
Page Index Toggle Pages: 1