Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package jdpunkty;
  7.  
  8. import java.util.Random;
  9. import java.util.Scanner;
  10.  
  11. /**
  12. *
  13. * @author Artur
  14. */
  15. public class Jdpunkty {
  16.  
  17. public static int liczbaPunktow;
  18. public static int liczbaTrafien = 0;
  19.  
  20. public static void main(String[] args) throws InterruptedException {
  21.  
  22. Scanner scanner = new Scanner(System.in);
  23. liczbaPunktow = scanner.nextInt();
  24.  
  25. int liczbaWatkow = 8;
  26. int liczbaLosowanNaWatek = liczbaPunktow / liczbaWatkow;
  27.  
  28. Thread[] watki = new Thread[liczbaWatkow];
  29.  
  30. for (int i = 0; i < 8; i++){ // startuje 8 watkow
  31.  
  32. Thread thread = new Thread(() -> {
  33.  
  34. Random r = new Random();
  35.  
  36. for (int j = 0; j < liczbaLosowanNaWatek; j++){ // kazdy watek losuje rowna ilosc razy (liczbaLosowanNaWatek razy)
  37.  
  38. double x = Math.abs(r.nextDouble()) % 1;
  39. double y = Math.abs(r.nextDouble()) % 1;
  40.  
  41. double odleglosc = (x*x + y*y);
  42.  
  43. if (odleglosc < 1){
  44.  
  45. liczbaTrafien ++;
  46.  
  47. }
  48.  
  49. }
  50.  
  51. });
  52.  
  53. watki[i] = thread;
  54.  
  55. thread.start();
  56.  
  57. }
  58.  
  59. for (int i = 0; i < liczbaWatkow; i++){
  60. watki[i].join();
  61. }
  62.  
  63. System.out.println("trafiono " + liczbaTrafien + " na " + liczbaPunktow + " punktow");
  64.  
  65. }
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement