Skip to content

Commit

Permalink
Implement basic RandomProperty functionality (#46, #36)
Browse files Browse the repository at this point in the history
  • Loading branch information
a5kin committed Mar 12, 2019
1 parent 0a37a77 commit a9c147f
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion xentica/core/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ class MyCA(core.CellularAutomaton):
"""
import math
import abc
import inspect
from collections import OrderedDict

import numpy as np
from cached_property import cached_property

from xentica.core.variables import DeferredExpression
from xentica.core.exceptions import XenticaException
from xentica.tools import xmath

__all__ = ['Property', 'IntegerProperty', 'ContainerProperty', ]

Expand Down Expand Up @@ -540,8 +542,25 @@ class RandomProperty(Property):
def __init__(self):
self._buf_num = 0
super(RandomProperty, self).__init__()
raise NotImplementedError

def __get__(self, obj, objtype):
"""Implement custom logic when property is get as class descriptor."""
self.declare_once()
self._get_next()
return self

def calc_bit_width(self):
"""Really dirty 16-bit PRNG."""
return 16

def _get_next(self):
"""Generate and return next value for RNG stream."""
val = ((DeferredExpression(self.var_name) * 58321 + 11113)) % 65535
self.__set__(self, val)
container = inspect.currentframe().f_back.f_locals['obj']
self.bsca.deferred_write(container)

@property
def uniform(self):
"""Get random value from uniform distribution."""
return xmath.float(DeferredExpression(self.var_name)) / 65535

0 comments on commit a9c147f

Please sign in to comment.