 ### Author Topic: NormRnd - standard normal random number (a tiny module)  (Read 1429 times)

#### eri0o ##### NormRnd - standard normal random number (a tiny module)
« on: 11 Aug 2017, 02:10 »
NormRnd

NormRnd is a tiny (2.1kB) module adding a standard normal random number function. It uses Box-Muller method, which should be reasonably fast.

It exposes two functions:

float normrnd_unitary()
Returns a standard normal random number, as a float, with median 0, and variance 1.

int normrnd_asint(int median, int sigma, int clamp_ceil,  int clamp_floor)
Returns a standard normal random number, as a int, requires passing a defined median and sigma. It also allow clamping to restrict the possible returning values. If clamp_ceil and clamp_floor are equal, clamping is disabled.

NormRnd.asc

Spoiler: Show
1. // normrnd
2. //
3. // This module allows returning standard Normal random number.
4. // This code is based on Box-Muller method, implemented in C
5. // by Prof Darren Wilkinson here:
6. //
7. // http://www.mas.ncl.ac.uk/~ndjw1/teaching/sim/transf/norm.html
8. //
9. // My work was porting to AGS and giving utilitary functions
10. // Author: Erico Porto
11. //
12.
13. float surand()
14. {
15.   return ( IntToFloat(Random(1000))/1000.0 );
16. }
17.
18. float urand(float low, float high)
19. {
20.   return(low+(high-low)*surand());
21. }
22.
23. float genexp(float lambda)
24. {
25.   float u,x;
26.   u=surand();
27.   x=(-1.0/lambda)*Maths.Log(u);
28.   return(x);
29. }
30.
31. float gennor()
32. {
33.   float theta,rsq,x;
34.   theta=urand(0.0,2.0*Maths.Pi);
35.   rsq=genexp(0.5);
36.   x=Maths.Sqrt(rsq)*Maths.Cos(theta);
37.   return(x);
38. }
39.
40. //normrnd_unitary
41. //
42. // returns a float that is a standard Normal random number
43. float normrnd_unitary(){
44.   return gennor();
45. }
46.
47. // returns an int that is a standard Normal random number
48. // first paramter, median, is the median of the distribution
49. // second parameter, sigma, is proportional to the variance of the distribution
50. // third and fourth parameter allow clamping the result if a hard limit is desired
51. int normrnd_asint(int median, int sigma, int clamp_ceil,  int clamp_floor){
52.   int return_value = FloatToInt(gennor()*IntToFloat(sigma)+IntToFloat(median));
53.   if( clamp_ceil != clamp_floor && clamp_ceil > clamp_floor){
54.     if (return_value > clamp_ceil){
55.       return_value = clamp_ceil;
56.     }
57.     if (return_value < clamp_floor){
58.       return_value = clamp_floor;
59.     }
60.   }
61.
62.   return return_value;
63. }
64.

NormRnd.ash

Spoiler: Show
1. // normrnd
2. //
3. // This module allows returning standard Normal random number.
4. // This code is based on Box-Muller method, implemented in C
5. // by Prof Darren Wilkinson here:
6. //
7. // http://www.mas.ncl.ac.uk/~ndjw1/teaching/sim/transf/norm.html
8. //
9. // My work was porting to AGS and giving utilitary functions
10. // Author: Erico Porto
11. //
12. import float normrnd_unitary();
13. import int normrnd_asint(int median, int sigma, int clamp_ceil = 0,  int clamp_floor = 0);
14. 