libnxter  0.1
RandomNorm.nxc
Go to the documentation of this file.
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  RandomNorm.nxc
4  Copyright (C) 2008 Naba Kumar <naba@gnome.org>
5  Daniele Benedettelli <danielebenedettelli@gmail.com>
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 
27 /* Normally distributed random numbers with mean 0 and variance 1, scaled by
28  * 100 to fit in integer. 256 samples.
29  */
30 
31 #ifndef __RANDOM_NORM_H_
32 #define __RANDOM_NORM_H_
33 
34 int nRandSamples[] = {
35 9, -95, -118, 27, -148, 0, 114, 39, -104, 208, 196, 22, -5, 0, -113, -126,
36 33, -11, 131, 50, -40, 175, -140, 292, -73, 68, -8, -46, -118, 95, -32, 79,
37 164, 138, 41, 16, -48, -32, -70, 20, -30, -169, 26, 64, -36, -76, -81, 201,
38 -80, -61, 0, -39, -108, 23, -59, -6, 35, 153, -15, -112, -9, 32, 151, -52,
39 -38, 28, 24, -24, -216, -12, -49, -2, -18, 133, 10, 77, -58, -240, -189, -36,
40 98, 98, 50, 131, 2, 0, -57, 108, -185, 30, -12, 43, 48, 89, 22, 21,
41 -63, -14, -6, 23, -58, 1, -86, -136, 174, -27, -97, 38, -178, 51, -80, 141,
42 -43, 49, 172, -112, 79, 64, 101, 23, -62, -242, 4, 152, 13, -201, 158, 60,
43 47, -66, 19, -70, 9, -125, 56, 11, -52, 90, -161, 167, 49, -77, -199, 91,
44 52, 203, -81, 26, -23, 45, 59, -20, 4, 32, -161, -4, -101, 118, 218, 47,
45 19, 114, 17, -146, -87, 26, 93, -102, 101, -18, 79, 59, 27, -28, -62, 175,
46 62, 19, 277, -87, -99, 33, 29, 131, -43, -88, -68, -31, -79, -120, 93, -121,
47 -59, -99, 35, -72, 56, 149, 37, -5, 32, 53, 11, -13, -136, 66, -185, -62,
48 138, 0, -8, 147, 41, -160, 82, -128, 59, 80, 106, 4, -196, 23, 111, -91,
49 -7, 45, 134, 78, 185, 44, -17, -41, 16, 100, 93, -20, -140, 113, 39, 102,
50 4, -72, -91, 183, 113, -191, 73, -26, -67, -91, -79, -217, 59, 12, 69, -117,
51 };
52 
53 int lastNrandIndex = 0;
54 
64 int RandomNorm(int scale)
65 {
66  lastNrandIndex++;
67  if (lastNrandIndex > 255)
68  lastNrandIndex = 0;
69  return (nRandSamples[lastNrandIndex] * scale) / 100;
70 }
71 
83 long RandomNorm2(int scale)
84 {
85  long nr = -600;
86  for (int i = 0; i < 12; i++)
87  {
88  nr += Random(100);
89  }
90  return ((nr * scale)/100);
91 }
92 
100 long RandomNorm3(int scale, int precision)
101 {
102  int iter = precision * 12;
103  long s;
104  long nr = (-iter * 100)/2;
105  for (int i = 0; i < iter; i++)
106  {
107  nr += Random(100);
108  }
109  s = (10000 * iter)/12;
110  s = Sqrt(s);
111  return ((nr * scale)/s);
112 }
113 
114 /* #define ENABLE_TEST */
115 
116 #ifdef ENABLE_TEST
117 
118 /* return maximum value in array */
119 int ArrayMax(long v[])
120 {
121  int len = ArrayLen(v);
122  int max = 0;
123  for(int i = 0; i < len; i++)
124  {
125  if (v[i] > max) max = v[i];
126  }
127  return max;
128 }
129 
130 #define HIST 30
131 #define SAMPLES 5000
132 void TestRandomNorm ()
133 {
134  long r;
135  long stat[HIST];
136  int progressbar, zone;
137  int i, x, w, h, max;
138 
139  TextOut(0,LCD_LINE1,"Normal Samples");
140  for (i = 0; i < SAMPLES; i++)
141  {
142  progressbar = (100 * i) / SAMPLES;
143  LineOut(0, LCD_LINE2 + 4, progressbar, LCD_LINE2 + 4);
144  LineOut(0, LCD_LINE2 + 3, progressbar, LCD_LINE2 + 3);
145  r = RandomNorm3(100,2);
146 
147  /* build histogram */
148  for (zone = 0; zone < HIST; zone++)
149  {
150  /* 3-sigma confidence */
151  if (-300 + zone * 600 / HIST <= r & r < -300 + (zone + 1) * 600 / HIST)
152  stat[zone]++;
153  }
154  }
155 
156  ClearScreen();
157  max = ArrayMax(stat);
158  w = 100/HIST - 1;
159  for (zone = 0; zone < HIST; zone++)
160  {
161  x = (100 * zone)/HIST;
162  h = (63 * stat[zone])/max;
163  RectOut(x, 0, w, h);
164  }
165  while(true);
166 }
167 /*
168 task main()
169 {
170  TestRandomNorm();
171 }
172 */
173 #endif /* ENABLE_TEST */
174 #endif /* __RANDOM_NORM_H_ */
175 
long RandomNorm3(int scale, int precision)
Returns a random number sample from Guassian distribution scaled by the given factor (using sum of un...
Definition: RandomNorm.nxc:100
long RandomNorm2(int scale)
Returns a random number sample from Guassian distribution scaled by the given factor (using sum of un...
Definition: RandomNorm.nxc:83
int RandomNorm(int scale)
Returns a random number sample from Guassian distribution scaled by the given factor (using lookup ta...
Definition: RandomNorm.nxc:64