samedi 7 février 2015

Random keeps returning the same numbers [duplicate]


This question already has an answer here:





var random = new Random();
Canvas.SetLeft(enemy, random.Next((int) (ImageCanvas.Width - 100)));
Canvas.SetTop(enemy, random.Next((int)(ImageCanvas.Height - 100)));


I'm using the above to randomly set the Left and Top positions of a rectangle on the Canvas. Oddly enough, once I call the method (there is other code too) and add the rectangle to the screen and then call a different method that uses the same code above, the second rectangle ends up being added directly on top of the last, which means that it is using the same random number for the Top and Left. Why is this? I thought calling random.Next in a totally different method would lead to a new random number.





Aucun commentaire:

Enregistrer un commentaire