philiprehberger-random_data 0.2.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6350aa4eda28ecdea9d193df38c56e81c86721fbd2b6942c5db449faf27d8edb
4
- data.tar.gz: 2d5344a60caa5109834c6092ad48cf8b5b98b668fb0202c799a87b54675b946b
3
+ metadata.gz: 66b3892c4c67cf116d9f415c2f15f9351e9fc9ecc907fcd052d93db04c2da69c
4
+ data.tar.gz: 25f7bd4d5907d4350dc5d9381781b6c47f694e4df593c48ef80347147fd97ad8
5
5
  SHA512:
6
- metadata.gz: 16d362cc134a11ccf60a5c93ce97bb1b418d8c16174592b8b602053f5ac91d92a1e58f9070c190a4904f39d921b5df73eafa86e08f1a94b204dd9731e6bad029
7
- data.tar.gz: 77700612c576d8c54f35915143eade1ac1f1dd5bde72463a13c8df90633888311ce71a88aefc1beedfe91fd5eca4f22fd987f02aff12adc72618049b16906211
6
+ metadata.gz: 4ea172d1596e1f5c28aa06cef63822fec2202f4a176c526f4f132a5aedd13bbb0db6bda54740ef11fbfbbafb6242e7b2693cc22f89e1a547fb09cf3fa241ccd7
7
+ data.tar.gz: 127842bc07223d27f397348d79ebec2dda96e32a588b13119748070bb89054b9719714e6744c33a3b0e57df82eda1eca154c7ebc116474a628e397e3da611367
data/CHANGELOG.md CHANGED
@@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.4.0] - 2026-04-24
11
+
12
+ ### Added
13
+ - `RandomData.coordinates` — returns a `{ latitude:, longitude: }` hash with latitude in `-90.0..90.0` and longitude in `-180.0..180.0`
14
+
15
+ ## [0.3.0] - 2026-04-16
16
+
17
+ ### Added
18
+ - `RandomData.weighted_pick(array, weights:)` — pick an element using cumulative-weight sampling proportional to the supplied non-negative numeric weights
19
+
10
20
  ## [0.2.1] - 2026-03-31
11
21
 
12
22
  ### Changed
data/README.md CHANGED
@@ -63,6 +63,14 @@ Philiprehberger::RandomData.pick(colors) # => "green"
63
63
  Philiprehberger::RandomData.sample(colors, 2) # => ["blue", "red"]
64
64
  ```
65
65
 
66
+ ### Weighted Pick
67
+
68
+ ```ruby
69
+ tiers = %w[bronze silver gold]
70
+ Philiprehberger::RandomData.weighted_pick(tiers, weights: [70, 25, 5])
71
+ # => "bronze" most of the time, "gold" rarely
72
+ ```
73
+
66
74
  ### Network
67
75
 
68
76
  ```ruby
@@ -70,6 +78,13 @@ Philiprehberger::RandomData.ipv4 # => "192.45.67.123"
70
78
  Philiprehberger::RandomData.url # => "https://smith42.com"
71
79
  ```
72
80
 
81
+ ### Coordinates
82
+
83
+ ```ruby
84
+ Philiprehberger::RandomData.coordinates
85
+ # => { latitude: 42.7651, longitude: -87.3419 }
86
+ ```
87
+
73
88
  ### Address and Company
74
89
 
75
90
  ```ruby
@@ -107,7 +122,9 @@ Philiprehberger::RandomData.timestamp # => 2025-08-14 03:22:11 +0000
107
122
  | `RandomData.hex(length)` | Random hex string |
108
123
  | `RandomData.pick(array)` | Random element from array |
109
124
  | `RandomData.sample(array, n)` | Random n elements from array |
125
+ | `RandomData.weighted_pick(array, weights:)` | Random element chosen proportionally to matching weights |
110
126
  | `RandomData.ipv4` | Random IPv4 address |
127
+ | `RandomData.coordinates` | Random `{ latitude:, longitude: }` pair |
111
128
  | `RandomData.address` | Random address hash with street, city, state, zip |
112
129
  | `RandomData.company` | Random company name |
113
130
  | `RandomData.url` | Random URL |
@@ -55,5 +55,34 @@ module Philiprehberger
55
55
  URL_SCHEMES = %w[https http].freeze
56
56
 
57
57
  URL_TLDS = %w[com org net io dev app co].freeze
58
+
59
+ # Pick a random element from +array+ using the matching probability +weights+.
60
+ #
61
+ # Each index in +array+ has a corresponding non-negative numeric weight in +weights+.
62
+ # The element is selected via cumulative-weight sampling, where elements with higher
63
+ # weights are proportionally more likely to be returned.
64
+ #
65
+ # @param array [Array] source array
66
+ # @param weights [Array<Numeric>] non-negative weights, one per element
67
+ # @return [Object] one element from +array+ chosen proportionally to its weight
68
+ # @raise [ArgumentError] if +array+ and +weights+ differ in length,
69
+ # if any weight is negative, or if every weight is zero
70
+ def self.weighted_pick(array, weights:)
71
+ raise ArgumentError, 'array and weights must be the same length' if array.length != weights.length
72
+ raise ArgumentError, 'weights must all be numeric' unless weights.all?(Numeric)
73
+ raise ArgumentError, 'weights must all be non-negative' if weights.any?(&:negative?)
74
+
75
+ total = weights.sum
76
+ raise ArgumentError, 'weights must not all be zero' if total.zero?
77
+
78
+ threshold = rand * total
79
+ cumulative = 0.0
80
+ array.each_with_index do |element, index|
81
+ cumulative += weights[index]
82
+ return element if threshold < cumulative
83
+ end
84
+
85
+ array.last
86
+ end
58
87
  end
59
88
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Philiprehberger
4
4
  module RandomData
5
- VERSION = '0.2.1'
5
+ VERSION = '0.4.0'
6
6
  end
7
7
  end
@@ -151,6 +151,18 @@ module Philiprehberger
151
151
  Array.new(4) { rand(1..254) }.join('.')
152
152
  end
153
153
 
154
+ # Generate a random geographic coordinate pair.
155
+ #
156
+ # Latitude is drawn uniformly from `-90.0..90.0` and longitude from
157
+ # `-180.0..180.0`. The distribution is not area-weighted — it samples
158
+ # the latitude axis uniformly, not over a sphere — which is usually
159
+ # what test fixtures want.
160
+ #
161
+ # @return [Hash{Symbol => Float}] `{ latitude:, longitude: }`
162
+ def self.coordinates
163
+ { latitude: float(-90.0..90.0), longitude: float(-180.0..180.0) }
164
+ end
165
+
154
166
  # Generate a random address
155
167
  #
156
168
  # @return [Hash] address with street, city, state, zip
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: philiprehberger-random_data
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Philip Rehberger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-03-31 00:00:00.000000000 Z
11
+ date: 2026-04-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Generate random test data including names, emails, phone numbers, UUIDs,
14
14
  sentences, paragraphs, dates, numbers, and more. Includes 50 first names, 50 last