Skip to main content

Python random numbers

Project description


Tasadofi - random python numerical types

This library wraps random number generation into classes inheriting from int and float. Every time a variable of random type is used, it has a different value, e.g. 1 + random_type will have different return value everytime. This allows to inject randomness into code without having to alter it, i.e. making numerical parameter of a class random without changing class itself. The random type also remembers last n generated random values.

Installation

pip install tasadofi

Example

Let us consider a demonstrative class that generates sine signal with fixed noise value.

import numpy as np
from tasadofi.random_number import RndFloat

class Signal:
    def __init__(self, noise=0.1):
        self.noise = noise
        
    def generate_signal(self, length=100):
        # code to generate some signal
        signal = np.linspace(0, length/2*np.pi, length)  # Example signal: a sine wave
        signal = np.sin(signal)  # Example signal: a sine wave
        signal += self.noise
        return signal

Then generated signal will be identical each repetation.

signal = Signal()
for _ in range(2):
    print(signal.generate_signal(length=5))
    # [ 0.1 1.02387953 -0.60710678 -0.28268343  1.1 ]
    # [ 0.1 1.02387953 -0.60710678 -0.28268343  1.1 ]

We can change this by using random type without touching underlying Signal class:

signal = Signal(noise=RndFloat('uniform', 0, 0.5))
for _ in range(2):
    print(signal.generate_signal(length=5))
    # [ 0.13548616  1.0593657  -0.57162062 -0.24719727  1.13548616]
    # [ 0.08913056  1.01301009 -0.61797623 -0.29355288  1.08913056]

We can also inspect last 10 generated values (or different number by using set_cache_size(n) function):

print(signal.noise)
# sampler uniform(0, 0.5)
# last 10 generated values [0.13548616468953223, 0.08913055526374658]

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

tasadofi-1.0.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tasadofi-1.0.1-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file tasadofi-1.0.1.tar.gz.

File metadata

  • Download URL: tasadofi-1.0.1.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.10.0 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/1.0.0 urllib3/1.26.20 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.13

File hashes

Hashes for tasadofi-1.0.1.tar.gz
Algorithm Hash digest
SHA256 776d194b3e6927a517dc172e8c7053675e8102a9d93d39fd161b6f0abd72fe51
MD5 0601064520ea255caf31d027e04640a9
BLAKE2b-256 c710d2736e9f38c311c57cc1b9fda346d4e8476f2a19b6bd6da55e3176dccd51

See more details on using hashes here.

File details

Details for the file tasadofi-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: tasadofi-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.10.0 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/1.0.0 urllib3/1.26.20 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.13

File hashes

Hashes for tasadofi-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b9866d768b64db245ec11c667188354e5babffcfac63430021dce4dea0e677ba
MD5 ecfb13166b09c3c7ae3532eea1d2d4f0
BLAKE2b-256 c8b3283331cb8f8ced3451d2d8b42dacdef4712a503ea47e016bc2c4ffc86da6

See more details on using hashes here.

Supported by

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