An implementation of the rand48 linear congruential generator.
Project description
pyrand48
pyrand48 is a partial implementation in Python of the rand48 linear congruential
generator. We currently omit seed48(), lcong48(), and nrand48(), but suggestions
or implementations are welcome.
Please note: pyrand48 is deterministic. It is not cryptographically secure
even with a good choice of starting seed. It is intended
for use by people who need uniformly distributed pseudo-random numbers in a
deterministic sequence.
Usage
from pyrand48.rand48 import Rand48
prng = Rand48(42) # seeds with int(time.time()) if no seed provided
random_float = prng.drand48() # 0 <= r < 1
random_unsigned_int = prng.lrand48() # 0 <= r < 2**31
random_signed_int = prng.mrand48() # -(2**31) <= r < 2**31
d6 = [1, 2, 3, 4, 5, 6]
d6_roll = d6[int(prng.drand48() * len(d6)]
Background
Linear congruential generators are pseudo-random number generators that convert an initial seed $X_0$ into a sequence of pseudo-random numbers $(X_0, X_1, X_2, ...)$ via the formula
$$X_{n+1} = (a X_n + c)\pmod{m}$$
Thus the multiplier $a$, the addend $c$, and the modulus $m$ uniquely specify a linear congruential generator. All computations are to be performed with integers modulo $m$.
The POSIX specification for rand48 specifies that rand48 is the linear congruential generator with
$$a = 5deece66d_{16},\quad c = b_{16},\quad m = 2^{48}.$$
The POSIX specification for rand48 specifies the following functions:
void srand48(long seedval);
unsigned short *seed48(unsigned short seed16v[3]);
void lcong48(unsigned short param[7]);
double drand48(void);
double erand48(unsigned short xsubi[3]);
long lrand48(void);
long nrand48(unsigned short xsubi[3]);
long mrand48(void);
long jrand48(unsigned short xsubi[3]);
This module prioritizes the rand48 linear congruential generator, with an expectation
of seeding with srand48. Consequently, we aim to make our functions conform with the
specification, but we do not aim for complete coverage of the functions available.
In particular:
- We omit
seed48()because of its use of an internal buffer. - We omit
lcong48because we believe a generic linear congruential generator should be preferred for this use. - Additionally, we omit
nrand48().
We include the C program generate_pyrand48_test.c which we used to generate conformance tests in Pytest.
License
Copyright (C) 2024 MLE Slone. Licensed under the MIT license.
This module includes a partial translation of Martin Birgmeier's implementation of the rand48 pseudorandom number generator from C into Python.
/*
* Copyright (c) 1993 Martin Birgmeier
* All rights reserved.
*
* You may redistribute unmodified or modified versions of this source
* code provided that the above copyright notice and this and the
* following conditions are retained.
*
* This software is provided ``as is'', and comes with no warranties
* of any kind. I shall in no event be liable for anything that happens
* to anyone/anything when using this software.
*/
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyrand48-0.1.10.tar.gz.
File metadata
- Download URL: pyrand48-0.1.10.tar.gz
- Upload date:
- Size: 3.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
61e457c0bddbdf6abe7567681603f38746da0b195582e3c8bbd8ecc276fcff0a
|
|
| MD5 |
446a52eef60e1c1f2193d998f0f018b8
|
|
| BLAKE2b-256 |
72ea70413861ca6dc51f078a9c612240703839d4a729bf9532f669a5389eefad
|
Provenance
The following attestation bundles were made for pyrand48-0.1.10.tar.gz:
Publisher:
release.yml on uklibraries/pyrand48
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyrand48-0.1.10.tar.gz -
Subject digest:
61e457c0bddbdf6abe7567681603f38746da0b195582e3c8bbd8ecc276fcff0a - Sigstore transparency entry: 1096804396
- Sigstore integration time:
-
Permalink:
uklibraries/pyrand48@e4dee583b903a596448ec03e49a57f0049c92e3d -
Branch / Tag:
refs/tags/v0.1.10 - Owner: https://github.com/uklibraries
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@e4dee583b903a596448ec03e49a57f0049c92e3d -
Trigger Event:
release
-
Statement type:
File details
Details for the file pyrand48-0.1.10-py3-none-any.whl.
File metadata
- Download URL: pyrand48-0.1.10-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e74ac2f0caeb2a84ff914a7411101297914c69793b9b6010c74681c085ad53c6
|
|
| MD5 |
1f20166e09111be2cbe175be66bd51fa
|
|
| BLAKE2b-256 |
b49bf146c62849030111c95b2ec3eee33f5d030cede09f0f4efd0df96989962e
|
Provenance
The following attestation bundles were made for pyrand48-0.1.10-py3-none-any.whl:
Publisher:
release.yml on uklibraries/pyrand48
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyrand48-0.1.10-py3-none-any.whl -
Subject digest:
e74ac2f0caeb2a84ff914a7411101297914c69793b9b6010c74681c085ad53c6 - Sigstore transparency entry: 1096804405
- Sigstore integration time:
-
Permalink:
uklibraries/pyrand48@e4dee583b903a596448ec03e49a57f0049c92e3d -
Branch / Tag:
refs/tags/v0.1.10 - Owner: https://github.com/uklibraries
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@e4dee583b903a596448ec03e49a57f0049c92e3d -
Trigger Event:
release
-
Statement type: