Problem with Random class in C#
-
HI, i am using Random class in my code like below private int RandomNumber(int min, int max) { Random random = new Random(); return random.Next(min, max); } and i am clalling this method in for loop. my expectation is for every iteration in the loop i am expecting some new random number. code for that is like below: string waybill= "W" + RandomNumber(10000, 90000).ToString(); Problem: everytime i am gettting same random number fr every iteration. instead of generating different random number. can any one help me , where i done the mistake.
fttyhtrhyfytrytrysetyetytesystryrty
-
HI, i am using Random class in my code like below private int RandomNumber(int min, int max) { Random random = new Random(); return random.Next(min, max); } and i am clalling this method in for loop. my expectation is for every iteration in the loop i am expecting some new random number. code for that is like below: string waybill= "W" + RandomNumber(10000, 90000).ToString(); Problem: everytime i am gettting same random number fr every iteration. instead of generating different random number. can any one help me , where i done the mistake.
fttyhtrhyfytrytrysetyetytesystryrty
The problem is, that new Random(); uses the current time to set the starting seed. If you call this in a too short time period twice it returns the same random number. Use something like this:
public class ...
{private int RandomNumber(int min, int max) { return m\_rndGen.Next(min, max); } private Random m\_rndGen = new Randow();
}
Greetings Covean
-
HI, i am using Random class in my code like below private int RandomNumber(int min, int max) { Random random = new Random(); return random.Next(min, max); } and i am clalling this method in for loop. my expectation is for every iteration in the loop i am expecting some new random number. code for that is like below: string waybill= "W" + RandomNumber(10000, 90000).ToString(); Problem: everytime i am gettting same random number fr every iteration. instead of generating different random number. can any one help me , where i done the mistake.
fttyhtrhyfytrytrysetyetytesystryrty
Don't instantiate a new Random every time. Treat random as a singleton. Random will (by design) give you the same sequence of numbers every time unless you start with a different seed. Right now you are getting the first number of the sequence every time.
Phil
The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.
-
HI, i am using Random class in my code like below private int RandomNumber(int min, int max) { Random random = new Random(); return random.Next(min, max); } and i am clalling this method in for loop. my expectation is for every iteration in the loop i am expecting some new random number. code for that is like below: string waybill= "W" + RandomNumber(10000, 90000).ToString(); Problem: everytime i am gettting same random number fr every iteration. instead of generating different random number. can any one help me , where i done the mistake.
fttyhtrhyfytrytrysetyetytesystryrty
Make the Randon instance a (static) member of the class.
-
HI, i am using Random class in my code like below private int RandomNumber(int min, int max) { Random random = new Random(); return random.Next(min, max); } and i am clalling this method in for loop. my expectation is for every iteration in the loop i am expecting some new random number. code for that is like below: string waybill= "W" + RandomNumber(10000, 90000).ToString(); Problem: everytime i am gettting same random number fr every iteration. instead of generating different random number. can any one help me , where i done the mistake.
fttyhtrhyfytrytrysetyetytesystryrty
The default constructor for the
Random
class uses the system clock to seed the random number generator. Your code is running fast enough that the system clock has not changed between calls to the constructor, resulting in the same random numbers being generated each iteration. See the MSDN documentation for the Random constructor[^] for more clarification and a good example.