Recent

Author Topic: Gaussian Random Number Generator  (Read 3453 times)

schuler

  • Full Member
  • ***
  • Posts: 223
Gaussian Random Number Generator
« on: May 20, 2018, 03:28:40 am »
:) Hello :)
Just done some googling and couldn't find any in pascal. Therefore, I ported a code from Java found here:
http://www.cs.princeton.edu/courses/archive/fall12/cos126/assignments/StdGaussian.java.html

This is how the ported code looks like:

Code: Pascal  [Select][+][-]
  1. // returns a random gaussivan value. This implementation is inspired on:
  2. //http://www.cs.princeton.edu/courses/archive/fall12/cos126/assignments/StdGaussian.java.html
  3. function TVolume.RandomGaussianValue: TNeuralFloat;
  4. var
  5.   r, x, y: TNeuralFloat;
  6. begin
  7.   r := 0;
  8.   // loop executed 4 / pi = 1.273.. times on average
  9.   while ( (r > 1) or (r = 0) ) do
  10.   begin
  11.     // find a uniform random point (x, y) inside unit circle
  12.     x := 2.0 * Random() - 1.0;
  13.     y := 2.0 * Random() - 1.0;
  14.     r := x*x + y*y;
  15.   end;
  16.  
  17.   RandomGaussianValue := x * Sqrt(-2.0 * Ln(r) / r);
  18. end;
  19.  
  20. procedure TVolume.RandomizeGaussian(pMul: TNeuralFloat = 1.0);
  21. var
  22.   I: integer;
  23.   vHigh: integer;
  24. begin
  25.   vHigh := High(FData);
  26.   for I := 0 to vHigh do
  27.     FData[I] := RandomGaussianValue() * pMul;
  28. end;
  29.  
  30. procedure TVolume.AddGaussianNoise(pMul: TNeuralFloat);
  31. var
  32.   I: integer;
  33.   vHigh: integer;
  34. begin
  35.   vHigh := High(FData);
  36.   for I := 0 to vHigh do
  37.     FData[I] += RandomGaussianValue() * pMul;
  38. end;
  39.  

This is how it can be tested:
Code: Pascal  [Select][+][-]
  1.   T2 := TNNetVolume.Create(550,1,1);
  2.   T2.RandomizeGaussian();
  3.   WriteLn('Gaussian Avg:',T2.GetAvg());
  4.   WriteLn('Gaussian Variance:',T2.GetVariance());
  5.  

The source code is maintained here:
https://sourceforge.net/p/cai/svncode/HEAD/tree/trunk/lazarus/libs/uvolume.pas

:) Wish everyone happy coding :)
« Last Edit: May 20, 2018, 03:45:20 am by schuler »

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Gaussian Random Number Generator
« Reply #1 on: May 20, 2018, 08:51:58 am »
There is already http://wiki.freepascal.org/Generating_Random_Numbers which includes Gaussian randoms.
« Last Edit: May 20, 2018, 08:57:21 am by Thaddy »
Specialize a type, not a var.

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: Gaussian Random Number Generator
« Reply #2 on: May 20, 2018, 09:33:56 am »
And in unit Math, there is also

Code: Pascal  [Select][+][-]
  1. function randg(mean,stddev : float) : float;  

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Gaussian Random Number Generator
« Reply #3 on: May 20, 2018, 09:39:21 am »
Indeed, I could not find that quick enough  :-X :'(
Specialize a type, not a var.

schuler

  • Full Member
  • ***
  • Posts: 223
Re: Gaussian Random Number Generator
« Reply #4 on: May 21, 2018, 09:03:49 am »
Thank you. Can't explain why previous googling didn't find it...  :'(
« Last Edit: May 21, 2018, 09:09:40 am by schuler »

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Gaussian Random Number Generator
« Reply #5 on: May 21, 2018, 09:16:26 am »
You still made a commendable effort. And by re-inventing the wheel you made sure it was round.. 8-) Happens all the time.
Specialize a type, not a var.

 

TinyPortal © 2005-2018