Hi,
I am trying to generate two different random numbers to display two different dice numbers on two image controls. The btn click event had the following code(below), every time I click the btn click event, both image controls show the same dice number. Please advise how to display different dice numbers on each image control?
Example, imgDDiceOne.Image will show dice number 5, imgDDiceOne.Image will show dice number 1.
Random rd1 = new Random();
Random rd2 = new Random();
int rol1 = rd1.Next(1, 6);
int rol2 = rd2.Next(1, 6);
imgDDiceOne.Image = Image.FromFile(filepath + "die-" + rol1.ToString() + ".gif");
imgDDiceTwo.Image = Image.FromFile(filepath + "die-" + rol2.ToString() + ".gif");
thanks
Loading
VulpesPosted Oct 6, 2012, 1:05 PM
to get a random number from 1 to 6, as the upper bound of the Next method is exclusive.
mengPosted Oct 6, 2012, 1:35 PM
VulpesPosted Oct 6, 2012, 12:53 PM
Try changing this line:
Random rd2 = new Random();
to this:
Random rd2 = new Random(Int32.MaxValue/2);
I'd also consider making the two Random variables, rd1 and rd2, into private fields (i.e. form level variables) rather than local variables. If you do that, then you won't be creating new objects each time the dice are rolled.
mengPosted Oct 6, 2012, 12:34 PM
Scott LyslePosted Oct 5, 2012, 9:57 PM