Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Reproduceral random generator, originally written by Niklas Rosenstein.
- Licensed under the WTFPL (c) - Do What The Fuck 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 Fuck 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, accoring 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 '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, accoring 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()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement