0

There is a strange problem with some of my code. I've created a list of some objects; when trying to fill this list with the new objects, if I use F5, all the members of the list will be created exactly the same as each other!

But when I try to debug this problem by F11, then the members of the list will be created different from each other (which is correct). I'm using a loop to generate all members of the list, and in each iteration of the loop, I use new to first instantiate the object of the class and then add it to the list.

I define the list as:

  1. List<MyClass> classList = new List<MyClass>();
  2. MyClass contains some user defined members and methods

I could not figure out why F5 does not fill the list correctly. I tried to replicate the same problem with other classes that contain only string and integer members, but that works properly.

Supun Wijerathne
  • 11,964
  • 10
  • 61
  • 87
  • Are you perhaps generating the elements with a `new Random` object each time? Or using different threads? –  Jul 12 '12 at 04:17
  • 4
    You'll need to post the relevant code if you want anybody to have a chance of figuring this out... – Eric J. Jul 12 '12 at 04:17
  • Please show some more code. The loop will be of particular interest. To format the code, add a blank line before the first line of the code, and indent the fragment by four additional spaces. – Sergey Kalinichenko Jul 12 '12 at 04:21
  • It could be caused by the different speeds of F5 and F11. Since F11 steps finer, more steps and thus in general more time is required. Meanwhile something on a different thread could have influenced your list elements - a behavior you would not see if the generation happened faster (like with F5). Just a theory... – RolandK Jul 12 '12 at 05:31
  • @Charmander yes I use new Random in each iteration of the loop! Actually it is not just F11 that works properly, if I put a break point somewhere in the loop and then use F5, it works fine! – abbas BozorgiRad Jul 12 '12 at 18:27
  • the code is: List population = new List(); for (int intIndex = 0; intIndex < 100; intIndex++) { Solution tempSolution = new Solution(); tempSolution.IsEqualTo(RandomChromosome(InitSolution)); population.Add(tempSolution); } the method RandomChromosome(InitSolution), takes an initial solution as the input and generates a random solution. the method IsEqualTo sets the tempSolution equal to the new randomly generated solution – abbas BozorgiRad Jul 12 '12 at 18:27
  • thanks guys, I found the answer: http://stackoverflow.com/questions/5398336/random-number-generator-always-picks-the-same-value-when-run-inside-a-loop – abbas BozorgiRad Jul 12 '12 at 19:33
  • @abbasBozorgiRad - Please update your question instead of using comments to post code. – Security Hound Jul 12 '12 at 19:40

1 Answers1

0

The problem is because of using new Random() method in each iteration of the loop. The correct way to do it is to use for example

rndNumber=new Random() 

once out of the loop, and then in the loop only use the

rndNumber.Next();

More information on the solution can be find in the following link:
Random number generator always picks the same value when run inside a loop

Community
  • 1
  • 1