tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
52
Task/Probabilistic-choice/Java/probabilistic-choice-2.java
Normal file
52
Task/Probabilistic-choice/Java/probabilistic-choice-2.java
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
import java.util.EnumMap;
|
||||
|
||||
public class Prob {
|
||||
public static long TRIALS= 1000000;
|
||||
public enum Glyph{
|
||||
ALEPH, BETH, GIMEL, DALETH, HE, WAW, ZAYIN, HETH;
|
||||
}
|
||||
|
||||
public static EnumMap<Glyph, Double> probs = new EnumMap<Glyph, Double>(Glyph.class){{
|
||||
put(Glyph.ALEPH, 1/5.0);
|
||||
put(Glyph.BETH, 1/6.0);
|
||||
put(Glyph.GIMEL, 1/7.0);
|
||||
put(Glyph.DALETH, 1/8.0);
|
||||
put(Glyph.HE, 1/9.0);
|
||||
put(Glyph.WAW, 1/10.0);
|
||||
put(Glyph.ZAYIN, 1/11.0);
|
||||
put(Glyph.HETH, 1759./27720);
|
||||
}};
|
||||
|
||||
public static EnumMap<Glyph, Double> counts = new EnumMap<Glyph, Double>(Glyph.class){{
|
||||
put(Glyph.ALEPH, 0.);put(Glyph.BETH, 0.);
|
||||
put(Glyph.GIMEL, 0.);put(Glyph.DALETH, 0.);
|
||||
put(Glyph.HE, 0.);put(Glyph.WAW, 0.);
|
||||
put(Glyph.ZAYIN, 0.);put(Glyph.HETH, 0.);
|
||||
}};
|
||||
|
||||
public static void main(String[] args){
|
||||
System.out.println("Target probabliities:\t" + probs);
|
||||
for(long i = 0; i < TRIALS; i++){
|
||||
Glyph choice = getChoice();
|
||||
counts.put(choice, counts.get(choice) + 1);
|
||||
}
|
||||
|
||||
//correct the counts to probablities in (0..1]
|
||||
for(Glyph glyph:counts.keySet()){
|
||||
counts.put(glyph, counts.get(glyph) / TRIALS);
|
||||
}
|
||||
|
||||
System.out.println("Actual probabliities:\t" + counts);
|
||||
}
|
||||
|
||||
private static Glyph getChoice() {
|
||||
double rand = Math.random();
|
||||
for(Glyph item:Glyph.values()){
|
||||
if(rand < probs.get(item)){
|
||||
return item;
|
||||
}
|
||||
rand -= probs.get(item);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue