Skip to content

Commit f29f490

Browse files
committed
feat: expose new generation methods in public API
- uniform(), gauss(), triangular(), exponential() - weighted_choice(), random_uuid(), random_token(), random_password()
1 parent 05ef977 commit f29f490

1 file changed

Lines changed: 150 additions & 0 deletions

File tree

src/trueentropy/__init__.py

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,146 @@ def sample(seq: Sequence[T], k: int) -> list[T]:
242242
return _tap.sample(seq, k)
243243

244244

245+
def uniform(a: float, b: float) -> float:
246+
"""
247+
Generate a random float N such that a <= N <= b.
248+
249+
Args:
250+
a: Lower bound
251+
b: Upper bound
252+
253+
Returns:
254+
Random float in [a, b]
255+
"""
256+
return _tap.uniform(a, b)
257+
258+
259+
def gauss(mu: float = 0.0, sigma: float = 1.0) -> float:
260+
"""
261+
Generate a random float from the Gaussian (normal) distribution.
262+
263+
Args:
264+
mu: Mean of the distribution (default: 0.0)
265+
sigma: Standard deviation (default: 1.0)
266+
267+
Returns:
268+
Random float from N(mu, sigma^2)
269+
"""
270+
return _tap.gauss(mu, sigma)
271+
272+
273+
def triangular(low: float = 0.0, high: float = 1.0, mode: float | None = None) -> float:
274+
"""
275+
Generate a random float from the triangular distribution.
276+
277+
Args:
278+
low: Lower limit (default: 0.0)
279+
high: Upper limit (default: 1.0)
280+
mode: Peak of the distribution. If None, defaults to midpoint.
281+
282+
Returns:
283+
Random float from the triangular distribution
284+
"""
285+
return _tap.triangular(low, high, mode)
286+
287+
288+
def exponential(lambd: float = 1.0) -> float:
289+
"""
290+
Generate a random float from the exponential distribution.
291+
292+
Args:
293+
lambd: Rate parameter (1/mean). Must be positive.
294+
295+
Returns:
296+
Random float from Exp(lambda)
297+
"""
298+
return _tap.exponential(lambd)
299+
300+
301+
def weighted_choice(seq: Sequence[T], weights: Sequence[float]) -> T:
302+
"""
303+
Return a random element from a sequence with weighted probabilities.
304+
305+
Elements with higher weights are more likely to be selected.
306+
307+
Args:
308+
seq: A non-empty sequence
309+
weights: Weights for each element (must be same length as seq)
310+
311+
Returns:
312+
A randomly selected element
313+
314+
Example:
315+
>>> trueentropy.weighted_choice(['rare', 'common'], [1, 10])
316+
'common' # Most likely
317+
"""
318+
return _tap.weighted_choice(seq, weights)
319+
320+
321+
def random_uuid() -> str:
322+
"""
323+
Generate a random UUID (version 4).
324+
325+
Returns:
326+
A UUID string in the format 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
327+
328+
Example:
329+
>>> trueentropy.random_uuid()
330+
'f47ac10b-58cc-4372-a567-0e02b2c3d479'
331+
"""
332+
return _tap.random_uuid()
333+
334+
335+
def random_token(length: int = 32, encoding: str = "hex") -> str:
336+
"""
337+
Generate a random token string.
338+
339+
Args:
340+
length: Number of random bytes to use (default: 32)
341+
encoding: Output encoding - 'hex' or 'base64' (default: 'hex')
342+
343+
Returns:
344+
A random token string
345+
346+
Example:
347+
>>> trueentropy.random_token(16, 'hex')
348+
'a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6'
349+
"""
350+
return _tap.random_token(length, encoding)
351+
352+
353+
def random_password(
354+
length: int = 16,
355+
charset: str | None = None,
356+
include_uppercase: bool = True,
357+
include_lowercase: bool = True,
358+
include_digits: bool = True,
359+
include_symbols: bool = True
360+
) -> str:
361+
"""
362+
Generate a secure random password.
363+
364+
Args:
365+
length: Password length (default: 16)
366+
charset: Custom character set (overrides include_* flags)
367+
include_uppercase: Include A-Z (default: True)
368+
include_lowercase: Include a-z (default: True)
369+
include_digits: Include 0-9 (default: True)
370+
include_symbols: Include !@#$%^&*()_+-= (default: True)
371+
372+
Returns:
373+
A random password string
374+
375+
Example:
376+
>>> trueentropy.random_password(12)
377+
'Kx9#mP2$nL7@'
378+
"""
379+
return _tap.random_password(
380+
length, charset, include_uppercase, include_lowercase,
381+
include_digits, include_symbols
382+
)
383+
384+
245385
# =============================================================================
246386
# PUBLIC API - Entropy Management
247387
# =============================================================================
@@ -384,6 +524,16 @@ def get_tap() -> EntropyTap:
384524
"randbytes",
385525
"shuffle",
386526
"sample",
527+
# Distributions
528+
"uniform",
529+
"gauss",
530+
"triangular",
531+
"exponential",
532+
"weighted_choice",
533+
# Generators
534+
"random_uuid",
535+
"random_token",
536+
"random_password",
387537
# Entropy management
388538
"health",
389539
"feed",

0 commit comments

Comments
 (0)