Functioning of Random, C#

justhelen

Client
Регистрация
18.11.2009
Сообщения
267
Благодарностей
134
Баллы
43
Hi, I have a question about working of Random in C#. Say I want to execute some code if variable i == 0. I have the following code:

Код:
Random rnd = new Random();
int i = rnd.Next(5);
if (i == 0){
   //code to execute
}
So, I would execute the code one time per 5 runnings of the template. And what if I had another code:

Код:
Random rnd = new Random();
for (int j = 0; j < 10; j++){
    int i = rnd.Next(50);
    if (i == 0){
       //code to execute
    }
}
Would I have the same result in the final? (execution of the code one time per 5 runnings of the template)
 

sMax

Client
Регистрация
04.05.2011
Сообщения
132
Благодарностей
49
Баллы
28
Hi,
I'm assuming you know that it may give you 5 times in a row.
So on average (on a statistically large enough scale), you would get 1 in 5.

Because (all) random functions are based on time.
I would add a random (sleep) timer before it cycles through the loop again (to break up patterns caused by code length depending of course on what is done in "//code to execute" for example).
Not strictly necessary, but it does break up some (RND) patterns noticed.
If this is embedded in zennoposter I would run one instance per base, and cleanup.

Hope that helps.
 

Кто просматривает тему: (Всего: 1, Пользователи: 0, Гости: 1)