Create your random object static
public class MyClass
{
public static Random rand = new Random();
public int MyMethod()
{
return rand.Next() % 10 + 1;
}
}
Random works on System.DatTime.Now.Ticks
.
If we do like this
Random rand = new Random();
internally it happens as
Random rand = new Random(System.DateTime.Now.Ticks);
Just think for a moment the only thing which is not constant in system is System Time.
When ever using Random class make its object once and use its method Next()
where ever you want. You will find this situation in loops when random object is created inside loops.
In your code they are created one after another, they get created by same Ticks seed value.
Create your random object static and then they won't be same.