Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.function.*;
- import java.util.stream.*;
- import static java.util.stream.Collectors.*;
- import java.util.*;
- public class MonteCarlo{
- private final static int N = 10_000_000;
- private final static Random r = new Random();
- public static Map<Integer, Double> parallelDiceRolls() {
- double fraction = 1.0 / N;
- return IntStream.range(0, N)
- .parallel()
- .mapToObj(n -> twoDiceThrows())
- .collect(groupingBy(side -> side, summingDouble(n -> fraction)));
- }
- public static int twoDiceThrows() {
- return r.nextInt(6) + r.nextInt(6) + 2;
- }
- public static void main(String []args){
- System.out.println(parallelDiceRolls());
- }
- }
Add Comment
Please, Sign In to add comment