Efficiently initialize and iterate a cyclic prng.
Project description
CyclicPRNG
A cyclical PRNG implementation that can provably generate every number in the cycle exactly once before restarting. For more information on why this was built and how it works, see the corresponding natlas documentation.
Mathematical Background
In short, CyclicPRNG uses a multiplicative cyclic group of prime order, and computes the factorization of the order of the group to obtain possible subgroup orders. CyclicPRNG uses these subgroup orders for generator testing, and finds random generators. CyclicPRNG uses these generators to determine the order to traverse the cycle.
Installation
pipenv install cyclicprng
Usage
Initializing a Reordering PRNG
from cyclicprng import CyclicPRNG
cycle_size = 10
c = CyclicPRNG(cycle_size)
first_cycle = [c.get_random() for _ in range(cycle_size)]
second_cycle = [c.get_random() for _ in range(cycle_size)]
assert first_cycle != second_cycle
Initializing a Consistent PRNG
from cyclicprng import CyclicPRNG
cycle_size = 10
c = CyclicPRNG(cycle_size, consistent=True)
first_cycle = [c.get_random() for _ in range(cycle_size)]
second_cycle = [c.get_random() for _ in range(cycle_size)]
assert first_cycle == second_cycle
Registering Event Handlers
CyclicPRNG allows you to register event handlers to receive messages when the PRNG restarts. Optionally, an event handler could be provided at initialization to also receive an init
event.
The two events emitted by CyclicPRNG are: init
and restart
.
>>> from cyclicprng import CyclicPRNG
>>> cycle_size = 5
>>> c = CyclicPRNG(cycle_size, event_handler=print)
init
>>> for i in range(cycle_size):
... print(c.get_random())
...
2
3
1
5
restart
4
>>> c.unregister_event_handler(print)
>>> c.notify
[]
>>> c.register_event_handler(print)
>>> c.notify
[<built-in function print>]
>>> c.clear_event_handlers()
>>> c.notify
[]
You may notice that the restart
event triggers before the last value is returned. This happens because the cycle automatically restarts when it reaches the final value in the cycle, right before that value is returned to the caller.
Edge Cases
The following edge cases exist for CyclicPRNG:
size < 1
-> Raise ValueErrorsize = 1
-> Always return 1size = 2
-> The generator is always 2 (equivalent to consistent=true)
Performance
I've included these performance numbers based on tests on my development desktop. As such, they may not be indicative of performance on your machine.
Initialization
On my desktop, CyclicPRNG repeatedly initializes with a size of 2^128 (the size of the IPv6 address space) in 0.03-0.04 seconds. I've included results from 10 sample inits below.
>>> import timeit
>>> ip6_timer = timeit.Timer(
... "CyclicPRNG(340282366920938463463374607431768211456)",
... setup="from cyclicprng import CyclicPRNG",
... )
>>>
>>> ip6_timer.repeat(10, 1)
[0.03722329996526241, 0.0364310999866575, 0.03413929999805987, 0.034686600090935826, 0.03407520009204745, 0.033979699946939945, 0.034076200099661946, 0.03446660004556179, 0.03457159991376102, 0.03437739983201027]
Getting The Next Number
In a similar test, 10 subsequent calls to get_random()
all measured in the range of 3e-6
to 7e-6
(3 to 7 microseconds). This would iterate the entire IPv4 address space (2^32) in a little over 8 hours.
>>> random_timer = timeit.Timer(
... "c.get_random()",
... setup="from cyclicprng import CyclicPRNG; c=CyclicPRNG(340282366920938463463374607431768211456)"
... )
>>> random_timer.repeat(10, 1)
[5.499925464391708e-06, 7.0999376475811005e-06, 5.600042641162872e-06, 6.4999330788850784e-06, 5.499925464391708e-06, 4.200031980872154e-06, 6.200047209858894e-06, 6.7998189479112625e-06, 5.899928510189056e-06, 3.00002284348011e-06]
Testing
CyclicPRNG uses pytest for testing. You can run tests yourself by cloning this repo and setting up like so:
git clone https://github.com/natlas/cyclicprng.git
cd cyclicprng/
pipenv install --dev
pipenv run pytest tests.py
License
Copyright 2020 The Natlas Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file cyclicprng-0.1.0.tar.gz
.
File metadata
- Download URL: cyclicprng-0.1.0.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.1.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c74c41c68a6d1a7919a255d41d428033cdfbed28f7ea6b4549ae510af3e0dc5a |
|
MD5 | 5821c031f1c54298cb7f88d42681c79f |
|
BLAKE2b-256 | 6941d27628fe5de9423fd39de793426b440ad697550200c87703b905491f3be9 |
File details
Details for the file cyclicprng-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: cyclicprng-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.1.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b85834ff083e1078d716dcbda313de10c1e5139a3e4e16d8bc94ea48b6bf58e7 |
|
MD5 | 998e45c4655ce513990420dfecbfcdb4 |
|
BLAKE2b-256 | 9703471556659146856112c14e11a7bdec6b07d9f32a5c18c2e4930c7e4b5ca3 |