Friday, April 25, 2014

Random Function not working as expected.

I have one integer as follows:



int densityrandom = generateRandom();



And I want random values to be assigned to "densityrandom" every time. So I am passing value from generateRandom() to densityrandom. Method is as follows:



private static int generateRandom()
{
Random rnd = new Random();
int random = rnd.Next(1, 8);
return random;
}

Each time it is generating same value if put in the for loop.


Like



for(int i = 0; i<10; i++)
{
int densityrandom = generateRandom();
Console.WriteLine("int is" + densityrandom);
}



It is printing similar value 10 times. What is wrong with the code?




-.NET Forum


No comments:

Post a Comment