how to do something "by chance"?

myndeswx

Client
Регистрация
15.05.2017
Сообщения
436
Благодарностей
104
Баллы
43
Probably a simple question, but sometimes there is need to do something random by percentages, currently I am using such a solution, but I am sure there is something more elegant.
Here we generate a random number from 1 to 10, and only when it's 10 we pause for a longer time (10%), and if the number is 8 or 9 we pause for 5 sec. (20% chance).

Please share how you do this, thank you!

87336
 

Roman48

Client
Регистрация
28.02.2016
Сообщения
2 058
Благодарностей
745
Баллы
113

myndeswx

Client
Регистрация
15.05.2017
Сообщения
436
Благодарностей
104
Баллы
43
C#:
 Random rnd = Global.Classes.rnd;
lock (rnd)
{
// Random r = new Random ();
if (rnd.Next (1,100) <50) return "Yes";
else return "No";
}
Thank you for the response, but as I understand from the code it would return "yes" or "no" 50% of the time?
ahh, so the 50 is percentages, we just change that part. Very smart, thank you!
 

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