I want to filter a list of double values I want number of items in this list that are greater or equal than a random value
When I use random function inside the lambda expression the number of items founds doesn't correspond to the amount expected. When I put random function outside lamda expression, the code is working right here is not working version vs working version
not working code:
List<double> vecteur = new List<double> { 0.45, 0.5, 1 };
List<int> lstcompteurs = new List<int> { 0,0,0};
Random r = new Random();
for (int i = 0; i < 1000; i++) {
int index = vecteur.FindIndex(a => a > r.NextDouble());
lstcompteurs[index]++;
}
foreach (int cpt in lstcompteurs) {
Console.WriteLine(cpt);
}
Console.Read();
output : 448 288 264
we notice that 288 represents much more than 5% of the sample.
working code:
List<double> vecteur = new List<double> { 0.45, 0.5, 1 };
List<int> lstcompteurs = new List<int> { 0,0,0};
Random r = new Random();
for (int i = 0; i < 1000; i++) {
double b = r.NextDouble();
int index = vecteur.FindIndex(a => a > b);
lstcompteurs[index]++;
}
foreach (int cpt in lstcompteurs) {
Console.WriteLine(cpt);
}
Console.Read();
output: 443 48 509
As you see 48 represents indeed 5% of the sample.
I do not understand what is going on with the first version of my code
Aucun commentaire:
Enregistrer un commentaire