Advertisement
dburyak

Untitled

Jan 1st, 2023 (edited)
3,124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 2.13 KB | None | 0 0
  1. /* output
  2. x list:
  3.   min: 150
  4.   max: 649
  5.   avg: 399.57069
  6.   bucket 0 [150.0, 199.9]: 9978 (10.0%)
  7.   bucket 1 [199.9, 249.8]: 9863 (9.9%)
  8.   bucket 2 [249.8, 299.7]: 10173 (10.2%)
  9.   bucket 3 [299.7, 349.6]: 10101 (10.1%)
  10.   bucket 4 [349.6, 399.5]: 10010 (10.0%)
  11.   bucket 5 [399.5, 449.4]: 9876 (9.9%)
  12.   bucket 6 [449.4, 499.3]: 10039 (10.0%)
  13.   bucket 7 [499.3, 549.2]: 9817 (9.8%)
  14.   bucket 8 [549.2, 599.1]: 10153 (10.2%)
  15.   bucket 9 [599.1, 649.0]: 9801 (9.8%)
  16.  
  17.  
  18. y list:
  19.   min: 10
  20.   max: 789
  21.   avg: 398.78173
  22.   bucket 0 [10.0 , 87.9 ]: 10052 (10.1%)
  23.   bucket 1 [87.9 , 165.8]: 9993 (10.0%)
  24.   bucket 2 [165.8, 243.7]: 10103 (10.1%)
  25.   bucket 3 [243.7, 321.6]: 10034 (10.0%)
  26.   bucket 4 [321.6, 399.5]: 9943 (9.9%)
  27.   bucket 5 [399.5, 477.4]: 10139 (10.1%)
  28.   bucket 6 [477.4, 555.3]: 9926 (9.9%)
  29.   bucket 7 [555.3, 633.2]: 9769 (9.8%)
  30.   bucket 8 [633.2, 711.1]: 9985 (10.0%)
  31.   bucket 9 [711.1, 789.0]: 9930 (9.9%)
  32.  
  33.  
  34. done
  35. */
  36.  
  37.  
  38.  
  39. // -----------------------------------------------
  40. // groovy script
  41.  
  42. int getRandomNumber(int min, int max) {
  43.     new Random().nextInt(max - min) + min
  44. }
  45.  
  46. def num = 100_000
  47. def xList = (0..<num).collect { getRandomNumber(150, 800 - 150) }
  48. def yList = (0..<num).collect { getRandomNumber(10, 800 - 10) }
  49.  
  50. def buckets = 10
  51.  
  52. [[xList, 'x list'], [yList, 'y list']].each { list, name ->
  53.     println "${name}:"
  54.     def min = list.min()
  55.     def max = list.max()
  56.     println "  min: ${min}"
  57.     println "  max: ${max}"
  58.     println "  avg: ${list.average()}"
  59.     def bucketSize = (max - min) / buckets
  60.     buckets.times { bucketNum ->
  61.         def bucketMin = list.min() + bucketSize * bucketNum
  62.         def bucketMax = list.min() + bucketSize * (bucketNum + 1)
  63.         def bucketCount = list.findAll { it >= bucketMin && it < bucketMax }.size()
  64.         def bucketMinStr = bucketMin.round(1).toString().padRight(5)
  65.         def bucketMaxStr = bucketMax.round(1).toString().padRight(5)
  66.         def pcnt = (bucketCount / list.size() * 100).round(1)
  67.         println "  bucket ${bucketNum} [${bucketMinStr}, ${bucketMaxStr}]: ${bucketCount} (${pcnt}%)"
  68.     }
  69.  
  70.     2.times { println() }
  71. }
  72. println 'done'
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement