P
P
PavelVlas2020-06-16 19:59:20
.NET
PavelVlas, 2020-06-16 19:59:20

How to make a constant random call to a variable?

The variable randomPer constantly produces the same thing ... only if the console is restarted, there will be a new variable, and so on all the time. Restarting is not an option.

public static string[] okper= new string[]
{
"ok1",
"ok2"
}
public static string randomPer = okper[new Random().Next(0, okper.Length)];

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
freeExec, 2020-06-16
@PavelVlas

Don't nuke Random every time, make it static too.

E
Egor, 2020-07-02
@exorka

If you specify the Seed parameter in the Random class constructor and set it equal to, for example, the current ticks, then it will work even in your version. And if not, then really only the freeExec option
Random rnd = new Random((int)DateTime.Now.Ticks);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question