Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package jdpunkty;
- import java.util.Random;
- import java.util.Scanner;
- /**
- *
- * @author Artur
- */
- public class Jdpunkty {
- public static int liczbaPunktow;
- public static int liczbaTrafien = 0;
- public static void main(String[] args) throws InterruptedException {
- Scanner scanner = new Scanner(System.in);
- liczbaPunktow = scanner.nextInt();
- int liczbaWatkow = 8;
- int liczbaLosowanNaWatek = liczbaPunktow / liczbaWatkow;
- Thread[] watki = new Thread[liczbaWatkow];
- for (int i = 0; i < 8; i++){ // startuje 8 watkow
- Thread thread = new Thread(() -> {
- Random r = new Random();
- for (int j = 0; j < liczbaLosowanNaWatek; j++){ // kazdy watek losuje rowna ilosc razy (liczbaLosowanNaWatek razy)
- double x = Math.abs(r.nextDouble()) % 1;
- double y = Math.abs(r.nextDouble()) % 1;
- double odleglosc = (x*x + y*y);
- if (odleglosc < 1){
- liczbaTrafien ++;
- }
- }
- });
- watki[i] = thread;
- thread.start();
- }
- for (int i = 0; i < liczbaWatkow; i++){
- watki[i].join();
- }
- System.out.println("trafiono " + liczbaTrafien + " na " + liczbaPunktow + " punktow");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement