Advertisement
TeRackSito

Java Random Integer 0-99

Apr 21st, 2024
675
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.44 KB | None | 0 0
  1. package angel;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. public class RandomCounterMap extends Thread {
  7.     private int start;
  8.     private int end;
  9.     private long executionTime;
  10.     private Map<Integer, Integer> results;
  11.  
  12.     public RandomCounterMap(int start, int end) {
  13.         this.start = start;
  14.         this.end = end;
  15.         this.results = new HashMap<>();
  16.     }
  17.  
  18.     public Map<Integer, Integer> getResults() {
  19.         return results;
  20.     }
  21.  
  22.     public int getLength() {
  23.         int length = 0;
  24.         for (int value : results.values()) {
  25.             length += value;
  26.         }
  27.         return length;
  28.     }
  29.  
  30.     public long getExecutionTime() {
  31.         return executionTime;
  32.     }
  33.  
  34.     private int randomInt() {
  35.         return (int) (Math.random() * 100);
  36.     }
  37.  
  38.     @Override
  39.     public void run() {
  40.         long startTime = System.currentTimeMillis();
  41.         for (int i = start; i <= end; i++) {
  42.             int result = randomInt();
  43.             try {
  44.                 results.put(result, results.get(result) + 1);
  45.             } catch (NullPointerException e) {
  46.                 results.put(result, 1);
  47.             }
  48.         }
  49.         long endTime = System.currentTimeMillis();
  50.         executionTime = endTime - startTime;
  51.     }
  52.  
  53.     public static void main(String[] args) {
  54.         int numbers = 100_000_000;
  55.         int numThreads = 8;
  56.         int numbersPerThread = numbers / numThreads;
  57.  
  58.         RandomCounterMap[] calculators = new RandomCounterMap[numThreads];
  59.         for (int i = 0; i < numThreads; i++) {
  60.             int start = i * numbersPerThread + 1;
  61.             int end = (i + 1) * numbersPerThread;
  62.             calculators[i] = new RandomCounterMap(start, end);
  63.         }
  64.  
  65.         long startTime = System.currentTimeMillis();
  66.         for (RandomCounterMap calculator : calculators) {
  67.             calculator.start();
  68.         }
  69.  
  70.         for (RandomCounterMap calculator : calculators) {
  71.             try {
  72.                 calculator.join();
  73.             } catch (InterruptedException e) {
  74.                 e.printStackTrace();
  75.             }
  76.         }
  77.         long endTime = System.currentTimeMillis();
  78.  
  79.  
  80.  
  81.         int totalResults = 0;
  82.         for (int i = 0; i < calculators.length; i++) {
  83.             System.out.println("Thread " + i + " took " + calculators[i].getExecutionTime() + "ms");
  84.         }
  85.         System.out.println("Time taken: " + (endTime - startTime) + "ms");
  86.     }
  87. }
  88.  
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement