vendredi 17 juin 2016

Is Random.Next(min, max) bugged? [duplicate]

This question already has an answer here:

I'm using Random.Next(int minValue, int maxValue) to generate random numbers into that interval.

To check if I'm doing it properly, I've performed the next operation:

Generated 10 List<int>, where each element int is randomly generated by the mentioned fuction.

Then, I've run the program about 10 times, givin Next function always the same values:

minValue = 1;
maxValue = 3;

I've never obtained 3 as generated value. Saying that there are only 3 values availables, this doesn't seem correct. An example of my output:

Individuo 0: (1,2,2,2,2) Individuo 1: (2,1,2,2,2) Individuo 2: (2,2,2,1,1) Individuo 3: (2,1,1,2,2) Individuo 4: (1,2,2,1,2) Individuo 5: (2,1,2,1,2) Individuo 6: (2,2,1,2,2) Individuo 7: (1,1,1,2,2) Individuo 8: (2,1,1,2,2) Individuo 9: (1,1,2,2,2)

So, I've found myself needing to use as maxValue my desired maxValue+1.

So my question, is this function known to be bug?

PD: executable code

int index = 0;
int trayecto = 0;
List<int> individuo;

Random rnd = new Random();

for (index = 0; index < 10; index++)
{

    individuo = new List<int>();

    for (d = 0; d < Constants.D; d++)
    {
        trayecto = rnd.Next(1, 3);
        individuo.Add(trayecto);
    }

    Console.Write("Individuo {0}: ({1})\r\n", index, String.Join(",", individuo));

    poblacion.Add(individuo);

}




Aucun commentaire:

Enregistrer un commentaire