Advertisement
nux95

Reproduceral Random Values - C4D

Jul 2nd, 2011
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.23 KB | None | 0 0
  1. """
  2. Reproduceral random generator, originally written by Niklas Rosenstein.
  3.  
  4. Licensed under the WTFPL (c) - Do What The Fuck You Want To Public License.
  5.  
  6. This program is free software. It comes without any warranty, to
  7. the extent permitted by applicable law. You can redistribute it
  8. and/or modify it under the terms of the Do What The Fuck You Want
  9. To Public License, Version 2, as published by Sam Hocevar. See
  10. http://sam.zoy.org/wtfpl/COPYING for more details.
  11. """
  12.  
  13. import  c4d
  14.  
  15. from    c4d                 import Vector
  16. from    c4d.utils.noise     import C4DNoise
  17.  
  18.  
  19. class Random(object):
  20.     """
  21.    A reproduceral random generator based on c4d.utils.noise.C4DNoise,
  22.    comparable to COFFEE's Random class.
  23.    """
  24.  
  25.     def __init__(self):
  26.         """
  27.        Construct the Random object.
  28.        The seed defaults to 0, the noiseType to c4d.NOISE_DENTS.
  29.        """
  30.  
  31.         self.__seed         = 0                     # default seed
  32.         self.__noise        = C4DNoise(self.__seed) # reproduceral random generator
  33.         self.__noiseType    = c4d.NOISE_DENTS       # default noise
  34.         self.__num          = 0                     # number of calls
  35.  
  36.     def Init(self, seed, noiseType = None):
  37.         """
  38.        Initialize the object with a seed and a noisetype.
  39.        noiseType defaults to c4d.NOISE_DENTS.
  40.        """
  41.  
  42.         if noiseType is None:
  43.             noiseType       = c4d.NOISE_DENTS
  44.  
  45.         self.__seed         = seed
  46.         self.__noiseType    = noiseType
  47.         self.__num          = 0
  48.  
  49.         return True
  50.  
  51.     def Reset(self):
  52.         """
  53.        Call this method to notify the end of the random-sequence.
  54.        This enables reproducerality.
  55.        """
  56.  
  57.         self.__num          = 0
  58.         return True
  59.  
  60.     def Get01(self):
  61.         """
  62.        Returns an, accoring to the specified noisetype distributed,
  63.        pseudorandom value between 0 and 1.
  64.        """
  65.  
  66.         try:
  67.             return self.__noise.Noise(
  68.                 self.__noiseType,
  69.                 True,                                   # 2d mapping
  70.                                                         # ------------------------------------
  71.                 Vector(                                 # Cinema 4D R12.048 's C4DNoise object
  72.                     self.__num,                         # does not care about it's seed argument
  73.                     (self.__seed % 16) ** 2,            # given on initialisation, so, I'm using
  74.                     ((self.__seed / 4) ** 2) % 84232.   # my own way to 'fake' a seed.
  75.                 )                                       # -------------------------------------
  76.             )
  77.         finally:
  78.             self.__num   += 1
  79.  
  80.     def Get11(self):
  81.         """
  82.        Returns an, accoring to the specified noisetype distributed,
  83.        pseudorandom value between -1 and 1.
  84.        """
  85.  
  86.         return self.Get01() * 2 - 1
  87.  
  88.  
  89. def main():
  90.     r       = Random()
  91.     r.Init(0)
  92.     for i in xrange(30):
  93.         if i == 0:
  94.             print "First sequence: ",
  95.         elif not i % 3:
  96.             r.Reset()
  97.             print "\nSecquence after Reset(): ",
  98.         print round(r.Get01(), 3), "",
  99.     print
  100.  
  101.  
  102. if __name__ == "__main__":
  103.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement