lundi 27 avril 2015

c# how to generate random number depends on propabilities

I have a situation in which i must generate a random number, this number must be either zero or one

So, the code is something like this:

randomNumber = new Random().Next(0,1)

However, the business requirements state that there is just 10% profitability that the generated number is zero and 90% profitability that the generated number is 1

However can I include that profitability in generating the random number please?

What I thought of is:

  1. Generate array of integer that includes 10 zeros and 90 ones.
  2. Generate a random index between 1 and 100
  3. Take the value that corresponds to that index

but I don't know if this way is the correct way, plus, i think that c# should have something ready for it




Aucun commentaire:

Enregistrer un commentaire