G
Guest
Hi, I'm using the Random class to return 5 random numbers which then are
added to a string.
When I do it in a controls event, such as button_ click, the numbers are
random as expected, but when I call the random function as a method of a
class, all the numbers are the same for each call to the class.
For example:
(Button click calls the myRandomObject.ReturnValue method)
button click produces 5,5,5
next button click produces 7,7,7
third button click produces 2,2,2
This is basically what is being called when the myRandomObject.ReturnValue
method is called:
Random r = new Random();
string op = r.Next(48,58).ToString();
Why does this work in a button click event but not from a class method? I
would think that the Random instance would be destroyed with each call to the
method.
Any ideas?
Thanks for your help on this
Ant.
added to a string.
When I do it in a controls event, such as button_ click, the numbers are
random as expected, but when I call the random function as a method of a
class, all the numbers are the same for each call to the class.
For example:
(Button click calls the myRandomObject.ReturnValue method)
button click produces 5,5,5
next button click produces 7,7,7
third button click produces 2,2,2
This is basically what is being called when the myRandomObject.ReturnValue
method is called:
Random r = new Random();
string op = r.Next(48,58).ToString();
Why does this work in a button click event but not from a class method? I
would think that the Random instance would be destroyed with each call to the
method.
Any ideas?
Thanks for your help on this
Ant.