how to do something "by chance"?

  • Автор темы Автор темы myndeswx
  • Дата начала Дата начала

myndeswx

Client
Регистрация
15.05.2017
Сообщения
437
Реакции
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
 
  • Оценить
Реакции: myndeswx
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!
 

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