Reproduceral Random Values



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 02/07/2011 at 12:29, xxxxxxxx wrote:

    As Py4D does not have a Random - class like COFFEE, here is one:

    """  
    Reproduceral random generator, originally written by Niklas Rosenstein.  
      
    Licensed under the WTFPL (c) - Do What The f**k You Want To Public License.  
      
    This program is free software. It comes without any warranty, to  
    the extent permitted by applicable law. You can redistribute it  
    and/or modify it under the terms of the Do What The f**k You Want  
    To Public License, Version 2, as published by Sam Hocevar. See  
    http://sam.zoy.org/wtfpl/COPYING for more details.  
    """  
      
    import  c4d  
      
    from    c4d                 import Vector  
    from    c4d.utils.noise     import C4DNoise  
      
      
    class Random(object) :  
      """  
      A reproduceral random generator based on c4d.utils.noise.C4DNoise,  
      comparable to COFFEE's Random class.  
      """  
      
      def __init__(self) :  
          """  
          Construct the Random object.  
          The seed defaults to 0, the noiseType to c4d.NOISE_DENTS.  
          """  
      
          self.__seed         = 0                     # default seed  
          self.__noise        = C4DNoise(self.__seed) # reproduceral random generator  
          self.__noiseType    = c4d.NOISE_DENTS       # default noise  
          self.__num          = 0                     # number of calls  
      
      def Init(self, seed, noiseType = None) :  
          """  
          Initialize the object with a seed and a noisetype.  
          noiseType defaults to c4d.NOISE_DENTS.  
          """  
      
          if noiseType is None:  
              noiseType       = c4d.NOISE_DENTS  
      
          self.__seed         = seed  
          self.__noiseType    = noiseType  
          self.__num          = 0  
      
          return True  
      
      def Reset(self) :  
          """  
          Call this method to notify the end of the random-sequence.  
          This enables reproducerality.  
          """  
      
          self.__num          = 0  
          return True  
      
      def Get01(self) :  
          """  
          Returns an, according to the specified noisetype distributed,  
          pseudorandom value between 0 and 1.  
          """  
      
          try:  
              return self.__noise.Noise(  
                  self.__noiseType,  
                  True,                                   # 2d mapping  
                                                          # ------------------------------------  
                  Vector(                                 # Cinema 4D R12.048 Py4D 's C4DNoise object  
                      self.__num,                         # does not care about it's seed argument  
                      (self.__seed % 16) ** 2,            # given on initialisation, so, I'm using  
                      ((self.__seed / 4) ** 2) % 84232.   # my own way to 'fake' a seed.  
                  )                                       # ------------------------------------  
              )  
          finally:  
              self.__num   += 1  
      
      def Get11(self) :  
          """  
          Returns an, according to the specified noisetype distributed,  
          pseudorandom value between -1 and 1.  
          """  
      
          return self.Get01() * 2 - 1  
      
      
    def main() :  
      r       = Random()  
      r.Init(0)  
      for i in xrange(30) :  
          if i == 0:  
              print "First sequence: ",  
          elif not i % 3:  
              r.Reset()  
              print "\nSecquence after Reset() : ",  
          print round(r.Get01(), 3), "",  
      print  
      
      
    if __name__ == "__main__":  
      main()
    

    Should be self-explaining.

    Cheers,



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 03/07/2011 at 16:30, xxxxxxxx wrote:

    Very nice! Thanks for sharing!



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 04/07/2011 at 08:43, xxxxxxxx wrote:

    Hy Nux,

    thank you for sharing! Doesn't python come with a random class? Is there an advantage in using your version over the python default?

    Thank you,
    maxx



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 04/07/2011 at 09:10, xxxxxxxx wrote:

    Yes, the python's random module does not provide reproduceral random values.
    So, you can't reproduce the result of the execution before, because those are (well not really real) but real random values.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 04/07/2011 at 14:36, xxxxxxxx wrote:

    Hy there,

    when I set a seed on the random, I also get  reproducible pseudo-random values?

    Like so:

    >> random.seed(5)
    >>> random.random()
    0.6229016948897019
    >>> random.random()
    0.7417869892607294
    >>> random.seed(5)
    >>> random.random()
    0.6229016948897019
    >>> random.random()
    0.7417869892607294

    Cheers,
    maxx



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 04/07/2011 at 21:42, xxxxxxxx wrote:

    Now this was new to me. O.o
    How could I miss the seed - function ?
    Nice, thanks !


Log in to reply