10 Replies Latest reply on May 20, 2011 5:34 AM by Royi A

    Generating random numbers

    T Beddard Level 1
      A shader I am working on requires a random number generator but as far as I can see this isn't a core method in the current PB kernel. However, there is a super simple implementation described here:
      http://www.codeproject.com/KB/recipes/SimpleRNG.aspx

      which is based on some methods created by George Marsaglia detailed here:
      http://www.bobwheeler.com/statistics/Password/MarsagliaPost.txt

      The only problem is that they require bitwise operations that also aren't yet supported in PB. Does anyone have any suggestions alternatives for random number generation?
        • 1. Re: Generating random numbers
          T Beddard Level 1
          I've been playing around with this a little more and have managed to generate a -not very random- random sequence using the Linear Congruential Generator ( http://en.wikipedia.org/wiki/Linear_congruential_generator ).

          After seeding the generator the next number requires on feeding back in the previously generated number. As it isn't possible to set a variable when evaluating one pixel that will then be available when evaluating the next pixel I've had to precalculate the random number sequence and store it in an array in the evaluateDependents() method.

          Unfortunately dependent arrays are limited to a maximum of 4096 elements so when used across all the pixels of an image you instantly see repeating patterns (see attached example code).

          Does anyone have any suggestions to improve this or alternative methods to generate random numbers in the Pixel Bender kernel?

          • 2. Re: Generating random numbers
            Kevin Goldsmith Level 3
            In a lot of our filters that require random numbers we use standard methods to generate a buffer of values and pass them into the kernel as a "noise" texture. This would work if you are targeting Flash. It looks like you aren't though. Someone else may have come up with a better solution...
            • 3. Re: Generating random numbers
              Squisterson

              This website offers a possibility: http://en.nicoptere.net/?p=302

               

              I use Pixel Bender mainly for Photoshop and the best way I would imagine to pass random numbers into Pixel Bender is to create a layer where every pixel has a random color, similar Add Noise. Question is: How would one go about doing that in Photoshop? Scripting? Filters? I would love the possibility to create such a noise texture, with full control about the number of colors (b/w, grey, RGB, RGBA) and a random number generator of my choice (so I can use normal distribution, when I feel like it). Any idea how that could be accomplished?

               

              Kevin, what methods do you use? Or were you referring only to Flash?

              • 4. Re: Generating random numbers
                unique_screenname_here Level 3

                You can script the Pixel Bender plugin via ExtendScript and I believe you can pass in values for parameters via that interface. You may be able to code up a simple pseudo-random number generator in ExtendScript and feed a Pixel Bender filter by passing the random numbers through parameters or as image data. I haven't tried this but I would expect it to work. In any case, let us know what you find out if you attempt it.

                • 5. Re: Generating random numbers
                  Squisterson Level 1

                  Yes, you can create random pixels with scripting. The only method I know is to select a 1x1 pixel area at a time and define a random color. That would take ages, even with a 800x600 image. Is there an easier way?

                   

                  As for feeding Pixel Bender parameters, wouldn't I need to define one parameters per random number? 800x600 random pixels = 480000 parameters? Or am I missing something? And as a third alternative, I've read somewhere that defining pseudo-random dependables inside Pixel Bender only let's you define something like 4096 values, which would cause undesired patterns, when used on a bigger image.

                   

                  I've started a thread on random pixel generation here: http://forums.adobe.com/message/3567740#3567740

                  • 6. Re: Generating random numbers
                    Christian Lett Level 3

                    In my opinion this feels like a bizarre ommission from the PB language. A Random() function should have been there since the start, and It's one of the items in the Wishlist thread I started a few weeks ago. But I'm sure the PB dev guys have good reason for not including it.

                     

                    As an After Effects PB developer I want to be able to generate noise via pseudo-random numbers within the kernel without having to resort to adding a noise layer and piping that into the plugin.

                     

                    None of this takes away how awesome PB is by the way - just hoping to get some extra bits of functionality in there!

                    • 7. Re: Generating random numbers
                      AIF Bob Level 3

                      But I'm sure the PB dev guys have good reason for not including it.

                       

                      The reason is that random functions and the operations required to easily support them, are not available on all of the hardware we support. We'd love to have random functions in there, but doing them well with our current hardware specs is extremely difficult.

                       

                      Bob

                      • 8. Re: Generating random numbers
                        Royi A Level 1

                        AIF Bob wrote:

                         

                        But I'm sure the PB dev guys have good reason for not including it.

                         

                        The reason is that random functions and the operations required to easily support them, are not available on all of the hardware we support. We'd love to have random functions in there, but doing them well with our current hardware specs is extremely difficult.

                         

                        Bob

                        Bob,

                        It's been few years since you launched PB.

                        I think it's time to drop support for old cards.

                         

                        Anything before nVidia 8XXX and ATI HD3XXX could easily be dropped.

                         

                        We need more features to get PB really going.

                         

                        Are you considering it?

                        • 9. Re: Generating random numbers
                          AIF Bob Level 3

                          Royi A wrote:

                          Are you considering it?

                           

                          Yes. We are always considering what our hardware specs are and what our base level of support is. It isn't purely our decision though, we also have to work with Photoshop and After Effects to make sure that GPUs they want to run on are still supported. I can't find the recording of Kevin Goldsmith's talk at nVidia's GTC last year but Kevin did post the slides here:

                           

                          http://blogs.adobe.com/kevin-goldsmith/2010/10/my-talk-from-the-2010-gtc-conference.html

                           

                          In particular, slides 12 through 16 look at what it takes to support a reasonable percentage of our customers.

                           

                          Bob

                          • 10. Re: Generating random numbers
                            Royi A Level 1

                            You don't support Intel cards so you gave up more than 50% of the market.

                             

                            Meaning you're targeting people with dedicated GPU.

                            Moreover, PB is a LAB feature, meaning the people trying it are "Pro Sumers" as you called them.

                             

                            Sum it all up and you get the picture, among the PB users those who use GPU which doesn't support DX10 are small small group.

                             

                            You should take advantage of that.