Skip to main content

Efficiently initialize and iterate a cyclic prng.

Project description


Last Commit Release License

Maintainability Total alerts pre-commit Code style: black

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.


pipenv install cyclicprng


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)
>>> for i in range(cycle_size):
...     print(c.get_random())
>>> 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 ValueError
  • size = 1 -> Always return 1
  • size = 2 -> The generator is always 2 (equivalent to consistent=true)


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.


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]


CyclicPRNG uses pytest for testing. You can run tests yourself by cloning this repo and setting up like so:

git clone
cd cyclicprng/
pipenv install --dev
pipenv run pytest


   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

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   See the License for the specific language governing permissions and
   limitations under the License.

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

cyclicprng-0.1.0.tar.gz (5.4 kB view hashes)

Uploaded source

Built Distribution

cyclicprng-0.1.0-py3-none-any.whl (9.3 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page