jeudi 7 octobre 2021

Random activity in a scheduled service

Context:

I have a background service which runs once a week and does some tasks (function A). But with a random probability I wish to introduce the possibility of running an another function (function B) in addition.

My thoughts:

I would just generate a number between [1,5] and if the number is 3 function B will run after function A.

Random _random = new Random(); 
int randomNumber = _random.Next(1, 5);  
if (randomNumber == 3)
{
 functionB();
}

Question:

Is there any better way to implement this? I wish to hear suggestions.

Thank you!




Aucun commentaire:

Enregistrer un commentaire