import java.util.Arrays ;
import java.util.List ;
import java.util.Random ;
import java.util.stream.Collectors ;
import java.util.stream.IntStream ;
class Main
{
static class RandomMethod {
private final int chance;
RandomMethod
( Runnable method,
int chance
) { this .method = method;
this .chance = chance;
}
public int getChance( ) { return chance; }
public void run( ) { method.run ( ) ; }
}
static class MethodChooser {
private final List< RandomMethod> methods;
private final int total;
MethodChooser( final List< RandomMethod> methods) {
this .methods = methods;
this .total = methods.stream ( ) .collect ( Collectors.summingInt ( RandomMethod:: getChance) ) ;
}
public void chooseMethod( ) {
final int choice = random.nextInt ( total) ;
int count = 0 ;
for ( final RandomMethod method : methods)
{
count += method.getChance ( ) ;
if ( choice < count) {
method.run ( ) ;
return ;
}
}
}
}
private static void aaa
( ) { System .
out .
println ( "a" ) ; } private static void bbb
( ) { System .
out .
println ( "b" ) ; } private static void ccc
( ) { System .
out .
println ( "c" ) ; }
public static void main
( String [ ] args
) { MethodChooser chooser
= new MethodChooser
( Arrays .
asList ( new RandomMethod( Main:: aaa, 1 ) ,
new RandomMethod( Main:: bbb, 3 ) ,
new RandomMethod( Main:: ccc, 1 )
) ) ;
IntStream.range ( 0 , 100 ) .forEach (
i -> chooser.chooseMethod ( )
) ;
}
}
aW1wb3J0IGphdmEudXRpbC5BcnJheXM7CmltcG9ydCBqYXZhLnV0aWwuTGlzdDsKaW1wb3J0IGphdmEudXRpbC5SYW5kb207CmltcG9ydCBqYXZhLnV0aWwuc3RyZWFtLkNvbGxlY3RvcnM7CmltcG9ydCBqYXZhLnV0aWwuc3RyZWFtLkludFN0cmVhbTsKCmNsYXNzIE1haW4KewoJc3RhdGljIGNsYXNzIFJhbmRvbU1ldGhvZCB7CiAgICAgICAgcHJpdmF0ZSBmaW5hbCBSdW5uYWJsZSBtZXRob2Q7CiAgICAgICAgcHJpdmF0ZSBmaW5hbCBpbnQgY2hhbmNlOwoKICAgICAgICBSYW5kb21NZXRob2QoUnVubmFibGUgbWV0aG9kLCBpbnQgY2hhbmNlKXsKICAgICAgICAgICAgdGhpcy5tZXRob2QgPSBtZXRob2Q7CiAgICAgICAgICAgIHRoaXMuY2hhbmNlID0gY2hhbmNlOwogICAgICAgIH0KCiAgICAgICAgcHVibGljIGludCBnZXRDaGFuY2UoKSB7IHJldHVybiBjaGFuY2U7IH0KICAgICAgICBwdWJsaWMgdm9pZCBydW4oKSB7IG1ldGhvZC5ydW4oKTsgfQogICAgfQogICAgCiAgICBzdGF0aWMgY2xhc3MgTWV0aG9kQ2hvb3NlciB7CiAgICAgICAgcHJpdmF0ZSBmaW5hbCBMaXN0PFJhbmRvbU1ldGhvZD4gbWV0aG9kczsKICAgICAgICBwcml2YXRlIGZpbmFsIGludCB0b3RhbDsKCiAgICAgICAgTWV0aG9kQ2hvb3NlcihmaW5hbCBMaXN0PFJhbmRvbU1ldGhvZD4gbWV0aG9kcykgewogICAgICAgICAgICB0aGlzLm1ldGhvZHMgPSBtZXRob2RzOwogICAgICAgICAgICB0aGlzLnRvdGFsID0gbWV0aG9kcy5zdHJlYW0oKS5jb2xsZWN0KENvbGxlY3RvcnMuc3VtbWluZ0ludChSYW5kb21NZXRob2Q6OmdldENoYW5jZSkpOwogICAgICAgIH0KCiAgICAgICAgcHVibGljIHZvaWQgY2hvb3NlTWV0aG9kKCkgewogICAgICAgICAgICBmaW5hbCBSYW5kb20gcmFuZG9tID0gbmV3IFJhbmRvbSgpOwogICAgICAgICAgICBmaW5hbCBpbnQgY2hvaWNlID0gcmFuZG9tLm5leHRJbnQodG90YWwpOwoKICAgICAgICAgICAgaW50IGNvdW50ID0gMDsKICAgICAgICAgICAgZm9yIChmaW5hbCBSYW5kb21NZXRob2QgbWV0aG9kIDogbWV0aG9kcykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgY291bnQgKz0gbWV0aG9kLmdldENoYW5jZSgpOwogICAgICAgICAgICAgICAgaWYgKGNob2ljZSA8IGNvdW50KSB7CiAgICAgICAgICAgICAgICAgICAgbWV0aG9kLnJ1bigpOwogICAgICAgICAgICAgICAgICAgIHJldHVybjsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KCQogICAgcHJpdmF0ZSBzdGF0aWMgdm9pZCBhYWEoKSB7IFN5c3RlbS5vdXQucHJpbnRsbigiYSIpOyB9CiAgICBwcml2YXRlIHN0YXRpYyB2b2lkIGJiYigpIHsgU3lzdGVtLm91dC5wcmludGxuKCJiIik7IH0KICAgIHByaXZhdGUgc3RhdGljIHZvaWQgY2NjKCkgeyBTeXN0ZW0ub3V0LnByaW50bG4oImMiKTsgfQoKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgICAgICBNZXRob2RDaG9vc2VyIGNob29zZXIgPSBuZXcgTWV0aG9kQ2hvb3NlcihBcnJheXMuYXNMaXN0KAogICAgICAgICAgICBuZXcgUmFuZG9tTWV0aG9kKE1haW46OmFhYSwgMSksCiAgICAgICAgICAgIG5ldyBSYW5kb21NZXRob2QoTWFpbjo6YmJiLCAzKSwKICAgICAgICAgICAgbmV3IFJhbmRvbU1ldGhvZChNYWluOjpjY2MsIDEpCiAgICAgICAgKSk7CgogICAgICAgIEludFN0cmVhbS5yYW5nZSgwLCAxMDApLmZvckVhY2goCiAgICAgICAgICAgIGkgLT4gY2hvb3Nlci5jaG9vc2VNZXRob2QoKQogICAgICAgICk7CiAgICB9Cn0=