A
A
AnotherAnkor2018-12-06 15:37:55
.NET
AnotherAnkor, 2018-12-06 15:37:55

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());
    }

from the loop I call the function that calls the one shown above, with the code:
int flag = 0;
      while(numberOfOperations > 0){
        numberOfOperations--;
        if (flag > 0) {
          flag--;
          opCredit+=debetReturn(account,bankBIC);
        }
        else {
          flag++;
          opDebet+=creditReturn(account,bankBIC);
        }
      }

This generates a sheet consisting of the same line.
Is it some kind of random number generator feature?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
AnotherAnkor, 2018-12-06
@AnotherAnkor

I found the answer to the question "what would be the right thing to do":

G
Griboks, 2018-12-06
@Griboks

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.

R
Roman Mirilaczvili, 2018-12-06
@2ord

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 question

Ask a Question

731 491 924 answers to any question