This is really strange, and I do not understand why this is happening. In the foreach loop, I repeat through a collection of class A, and for each class I call the Count() method, where the numbers r1 and r2 generated from the range [-1,1]. The problem is that Random.Next returns the same "random" numbers for each instance. When the results for the first instance are 0 and -1, the same will be returned from the following instances. Please could you tell me why this is happening? In addition, I cannot get different results in each instance of class A. This is the code:
class a { Random rnd = new Random(); private void Count() { int r1 = rnd.Next(-1, 1); int r2 = rnd.Next(-1, 1); } } class b { List<a> listofA=new list<a>(); foreach (a ACLASS in listofA) { ACLASS.Count(); } }
math c # random
Thomas Oct 31 '09 at 16:41 2009-10-31 16:41
source share