CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2005
    Posts
    41

    can anyone explain this line!

    X = (Y>>7 |1)/16777216.0 ;

    /*
    Y is long..X is float..
    the line will take teh value of Y and shift it 7 times then what?
    */

  2. #2
    Join Date
    Jul 2005
    Posts
    41

    Re: can anyone explain this line!

    i now get some more..
    he shift then or it with 1..but why 1 specificly?he wants to grant that teh least bit is 1!! why is that?

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: can anyone explain this line!

    Quote Originally Posted by deepinlife
    i now get some more..
    he shift then or it with 1..but why 1 specificly?he wants to grant that teh least bit is 1!! why is that?
    How are we supposed to know when we don't even know what the program is supposed to do?

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Jul 2005
    Posts
    41

    Re: can anyone explain this line!

    class randam_generator
    {
    /*Marse and roberts randrom generator algorthim*/
    private:
    const unsigned long MODLUS = 2147483647 ;
    const unsigned short MULT1 = 24112 ;
    const unsigned short MULT2 = 26143 ;

    const long zrng[]={1973272912};

    public:
    float lcgrand(int stram)
    {
    long zi , lowprd , hi3 ;
    zi =zrng[ stream ];
    lowprd =(zi & 65535 )* MULT1 ;
    hi31 = (zi >> 16 )*MULT1 +(lowprd >>16 );
    zi =( (lowprd & 65535) - MODUS )+ ( (hi31 & 32767)<<16)+(hi31>>15);
    if( zi <0)zi += MODUS ;
    lowprd =(zi &65535)*MULT2;
    hi31 =(zi >>16)*MULT2 +(lowprd >> 16 );
    zi = ( (lowprd & 65535)-MODLUS) +((hi31 &32767)<<16)+(hi31>>15);
    if(zi<0) zi+=MODLUS;
    zrng[stream] = zi;
    return (zi>>7|1)/16777216.0;
    }

    };

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: can anyone explain this line!

    So, it's an algorithm to generate pseudo-random numbers. If you don't understand the algorithm, search the web for the RNG's formula.

    What this particular line of code does is:
    • Y >> 7 : shifts Y to the left, 7 bit positions (that means Y is divided by 128)
    • (Y >> 7) | 1 : least significant bit of the result is set to one (if wasn't already)
    • ((Y >> 7) | 1) / 16777216.0 : the result (which is definitelly an odd number) is devided by a constant
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    Jul 2005
    Posts
    41

    Re: can anyone explain this line!

    thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured