Answer the question
In order to leave comments, you need to log in
Why are "random" values repeated when calling a function from a loop?
In the loop, I call the string generation function, which in turn already calls the random value generation function, as shown below:
/// <summary>
/// Генерация id операций.
/// </summary>
string createDocId() {
Random rnd = new Random();
return((rnd.Next(100000000, 999999999)).ToString());
}
int flag = 0;
while(numberOfOperations > 0){
numberOfOperations--;
if (flag > 0) {
flag--;
opCredit+=debetReturn(account,bankBIC);
}
else {
flag++;
opDebet+=creditReturn(account,bankBIC);
}
}
Answer the question
In order to leave comments, you need to log in
I found the answer to the question "what would be the right thing to do":
Random rnd = new Random(); generates a random number based on the current time. You call this function many times in a row, the time is the same - the results are repeated.
An instance of the Random class needs to be created only once.
https://stackoverflow.com/a/1785752/10118318
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question