1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| private class NbAleaUnif : IEnumerable
{
private double[] tab_alea; //tableau des nombres aléatoires
private int dimension; //dimension du tableau
public NbAleaUnif(int dim, string distribution) //constructeur
{
dimension = dim;
tab_alea = new double[dim];
Random alea = new Random(); //générateur de nombres aléatoires
if (distribution.Equals("uniform"))
{
for (int i = 0; i < dim; i++)
{
tab_alea[i] = alea.NextDouble();
}
}
if (distribution.Equals("gaussian"))
{
if((int)(dim%2)!=0)
dim++;
double[] buff = new double[dim];
double r1, r2, s;
for (int i=0; i < dim/2 ; i++)
{
do
{
r1 = 2 * alea.NextDouble() - 1;
r2 = 2 * alea.NextDouble() - 1;
s = r1 * r1 + r2 * r2;
} while (s > 1.0 || s == 0.0);
buff[2*i] = r1*Math.Sqrt(-2*Math.Log(s)/s);
buff[2*i+1] = r2*Math.Sqrt(-2*Math.Log(s)/s);
}
Array.Copy(buff,tab_alea,dimension);
}
}
public double Max { get { return tab_alea.Max(); } }
public double Min { get { return tab_alea.Min(); } }
public int dim { get { return dimension; } }
public IEnumerator GetEnumerator()
{
return new nombreAleatoireEnumerator(tab_alea);
}
private class nombreAleatoireEnumerator : IEnumerator
{
private int _index;
private double[] _alea;
public nombreAleatoireEnumerator(double[] numbers)
{
_alea = numbers;
_index = -1;
}
public void Reset()
{
_index = -1;
}
public bool MoveNext()
{
_index++;
return _index < _alea.Length;
}
public object Current
{
get { if(this._index < _alea.Length)
return _alea[_index];
else throw new IndexOutOfRangeException();
}
}
}
public void histo(int n,ref System.Windows.Forms.DataVisualization.Charting.Chart chart)
{
double[] _histo = new double[n];
double[] _X = new double[n];
double min = tab_alea.Min();
double max = tab_alea.Max();
double delta = (max-min)/n;
int indice;
for (int i = 0; i < n; i++)
{
indice = 0;
while (indice < tab_alea.Length)
{
if (tab_alea[indice] > (min + i * delta) && tab_alea[indice] < (min + (i + 1) * delta))
{
_histo[i]++;
}
indice++;
}
_X[i] = min + i * delta;
_histo[i] = _histo[i] / tab_alea.Length;
}
chart.Series.Add("tata");
chart.Series["tata"].Points.DataBindXY(_X, _histo);
}
} |