philiprehberger-random_data 0.2.1 → 0.3.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 +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +9 -0
- data/lib/philiprehberger/random_data/data.rb +29 -0
- data/lib/philiprehberger/random_data/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 954c7c0c37c43b00fb90d5ccd7a75803c317f8fd717c6901c8f373702a40e77a
|
|
4
|
+
data.tar.gz: 2b6a0b2f9c86e9f085e8131a96c254298725090ac235439da4e8942c4add65ec
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0b3e1f4502aace66e2a8411424d4068bfbebbf5e10667cc3ea0ccdfe9fdc3a2973eec52890db52d244d9ab5028fefa0397436cc7aa9aee4d66a14517c0fa3ffc
|
|
7
|
+
data.tar.gz: 3396c724469a9060ca1e945aa9b6673e351b34b190d24a4b1d5c9a07d272f8101c2404eec64ce8632d846be1e45f0809be15f769ebb3976d4b949db159d9f04f
|
data/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.3.0] - 2026-04-16
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
- `RandomData.weighted_pick(array, weights:)` — pick an element using cumulative-weight sampling proportional to the supplied non-negative numeric weights
|
|
14
|
+
|
|
10
15
|
## [0.2.1] - 2026-03-31
|
|
11
16
|
|
|
12
17
|
### 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
|
|
@@ -107,6 +115,7 @@ Philiprehberger::RandomData.timestamp # => 2025-08-14 03:22:11 +0000
|
|
|
107
115
|
| `RandomData.hex(length)` | Random hex string |
|
|
108
116
|
| `RandomData.pick(array)` | Random element from array |
|
|
109
117
|
| `RandomData.sample(array, n)` | Random n elements from array |
|
|
118
|
+
| `RandomData.weighted_pick(array, weights:)` | Random element chosen proportionally to matching weights |
|
|
110
119
|
| `RandomData.ipv4` | Random IPv4 address |
|
|
111
120
|
| `RandomData.address` | Random address hash with street, city, state, zip |
|
|
112
121
|
| `RandomData.company` | Random company name |
|
|
@@ -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
|
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.
|
|
4
|
+
version: 0.3.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-
|
|
11
|
+
date: 2026-04-16 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
|