Saturday, May 14, 2011

Random Class not Generating proper X and Y Coordinates

I am creating a snippet to change x and y coordinates upon runtime. However,not as i expected,the generated random numbers does not meet my requirements.
Here is the C# snippet:
Code:
  switch (odd_eve)  
  {  
   case 0:    
      Random rand = new Random();             
      count = rand.Next(0, 300);  
      break;  
  case 1:  
      Random rand1 = new Random();   
      count = rand1.Next(310, 600);  
      break;  
  default:  
  }  
Based from the code above,the random generates a slanting x and y coordinates. The solution i come up came from the idea of a fellow developer to declare one Random object in one class.
Code:
Random rand = new Random(); //declared as global variable  
 //function snippet  
 switch (odd_eve)  
 {  
  case 0:           
     count = rand.Next(0, 300);  
     break;  
 case 1:  
     count = rand.Next(310, 600);  
     break;  
 default:  
 }  
Works like a charm!!!

0 comments:

Post a Comment