mercredi 4 mai 2016

How to program contigencies that are executed with varying frequency?

Say I have 3 cases that I want them to be executed, but not always.

E.g.

Case 1 --> happens always
Case 2 --> happens 20% of times
Case 3 --> happens 60% of times

How can I go about to program this? Here's what I thought so far:

x = nextInt(100);
execute case 1
if( x <= 20 ){
 execute case 2
}
if ( x <= 60 ){
 execute case 3
}

Is there a different approach to this problem? Maybe some function that I'm unaware of? The java random documentation doesn't seem to have something for this.




Aucun commentaire:

Enregistrer un commentaire