Welcome to Phrogram Sign in | Join | Help
in Search


Perlin Noise generation in Phrogram

Last post 05-20-2008, 12:13 PM by christmaswhistler. 7 replies.
Sort Posts: Previous Next
  •  05-16-2008, 1:56 PM 6682

    Perlin Noise generation in Phrogram

    I found a good tutorial on Perlin Noise and I thought I might be able to implement it in Phrogram. I have a few problems though.

    His Noise function is designed to return a value between -1.0 and 1.0. The important bit is that it's a seeded number generator, meaning if you give it the same value more than once, the value it returns will remain constant. But it is written in C or some related language:

    function IntNoise(32-bit integer: x)

        x = (x<<13) ^ x;

        return ( 1.0 - ( (x * (x * x * 15731 + 789221) + 1376312589) & 7fffffff) / 1073741824.0);

    end IntNoise function

     

    There are 4 problems here:

    1.  x<<13  -  The bitwise left-shift operator. I read this article on bitwise operations, and I *think* it can be replaced with "X*(2*13)". Is this correct?
    2.  ^x  -  Bitwise XOR operator. This is really tricky. Phrogram does not go into bit values at all, so this will not work. Is there any logical/mathematical operation that does the same thing?
    3. & 7fffffff -  Bitwise AND operator. Don't think this would work either.
    4. 7fffffff  -  A hexadecimal value? How would this translate into a non-hexadecimal value? Would it be too large?

    I did a search for random number generators and I couldn't find any others.

  •  05-16-2008, 3:52 PM 6683 in reply to 6682

    Re: Perlin Noise generation in Phrogram

    1. Not correct. 13 shifts left means multiply by 2 13 times. so its the same as x*(2^13)

    2. You are quite correct. Phrogram doesn't have any bitwise operators.

    3. Ditto

    4. The Easiest way is to run windows calculator and put in in scientific mode. Then you can switch between hex and decimal. You can store up to 32 bit integers and each hex digit is 4 bits...so any 8 digit hex number will fit  7fffffff = 2147483647

    So how do we solve your problems. The best way is to find an random number algorithm that doesn't use bit wise operators

    http://en.wikipedia.org/wiki/List_of_random_number_generators

    This one is simple http://en.wikipedia.org/wiki/Blum_Blum_Shub

      Define seed As Integer = 3
      Define r As Integer = seed
      Define p As Integer = 181
      Define q As Integer = 137
      Define M As Integer = p * q
      
      While Not IsKeyDown(Escape)
       
       //Convert to the range -1....+1
       Define r2 As Decimal =r
       r2 = r2 / M * 2 - 1
       
       
       //Output
       Console.WriteLine(r + " " + r2)
       
       //Get next value
       r = (r * r) Mod M
       Delay(500)
      End While

    I would like to hear from people about their opinions on bitwise operators... seems an important part of learing about computers but also a fairly hard topic for a beginner to look at and understand.


    Managed DirectX and XNA ? Check out http://www.thezbuffer.com
  •  05-16-2008, 4:30 PM 6684 in reply to 6683

    Re: Perlin Noise generation in Phrogram

    I remember that:

    http://phrogram.com/forums/thread/758.aspx

    There is a workaround for  AND. I ( Or You ?! ) can do that for OR or ( and)  XOR .

    Michael

  •  05-16-2008, 5:56 PM 6685 in reply to 6684

    Re: Perlin Noise generation in Phrogram

    You know I debated writing such a helper but then I thought I would have to explain how it works... now you can explain ;-)
    Managed DirectX and XNA ? Check out http://www.thezbuffer.com
  •  05-17-2008, 5:29 AM 6686 in reply to 6683

    Re: Perlin Noise generation in Phrogram

    I thought that random numbers were generated by using the CPU clock in some case and via a "timer"...now know that is not the case. Although in some it appears that this is the "seed".

    Interesting...thanks! Think learning the "ins" and "outs" to everything certainly can make you a better programmer...

     


    TomDad. Tom is my son...and I'm his Dad. So we are TomDad Software.
  •  05-17-2008, 9:00 AM 6687 in reply to 6686

    Re: Perlin Noise generation in Phrogram

    "It depends" (c) (TM) The Zman 1996

     

    in .Net (for example) when you write:

         Random r = new Random()   (which is how you make a new random number generator

    It seeds it from some CPU timer... technically something you can't predict

    But you can also call one where you provide it with a seed

         Random r = new Random(1329)

     See http://msdn.microsoft.com/en-us/library/system.random.random.aspx

     

    If you seed with a constant value then for the same set of calls to that random number generator you will get the same series of random numbers. This is becuase there is no such thing as really random they are actually a mathematical series which seems random and is suuitably random for MOST situations.

    There are situatios such as security and gambling sites where the simple algorithms are not suitable and they use more complex ones

    See http://en.wikipedia.org/wiki/Pseudo_Random_Number_Generator

     

     

     


    Managed DirectX and XNA ? Check out http://www.thezbuffer.com
  •  05-17-2008, 9:01 AM 6688 in reply to 6687

    Re: Perlin Noise generation in Phrogram

    I will have to dig into the Phrogram code but I suspect Phrogram use the .Net version without a seed so it always starts at a different part.
    Managed DirectX and XNA ? Check out http://www.thezbuffer.com
  •  05-20-2008, 12:13 PM 6697 in reply to 6682

    Re: Perlin Noise generation in Phrogram

    Team Phrogram,

    I do not know if this helps (or hurts) but I re-wrote a random number generator for Boo! from a c-based algorithm that I found on the Internet.  It is complete with my assessment at the time that it is not a very good quality routine (2-Due-Low means it is on my to-do list and is low priority).  I ran it through a loop and plotted the points.  It *seemed* fairly distributed at the time to me - visually.  But, I have wanted to find and hack a better one.  Maybe somebody can start with this code and either prove it is high-quality or make improvements to it... and share back to the rest of us.

          Function RandomResult ( ) As Integer

                // Return a pseudo random integer between 1 and 100.

               

                // 2-Due-Low: It seems to work... but it is not pretty (find a better non-C based code sample)

                // Int rand()

                // {

                //  random_seed = random_seed * 1103515245 +12345;

                //  Return (unsigned Int)(random_seed / 65536) % 32768;

                // }

               

                Define Result As Integer

               

                GameRandomSeed = (GameRandomSeed * 1103515245) + 12345

                Result = abs(GameRandomSeed / 65536)

                Result = (Result / 328) + 1

                          

                Return Result

               

          End Function


    ChristmasWhistler
    http://www.christmaswhistler.com
    (\ _/)
    (- . -) This is bunneh. Copy and paste him to
    (")-(") your signature, so that he may gain popularity and eventually rule the world
View as RSS news feed in XML