R250 Quellcode

Maddimini

Mitglied
Hallo :),

ich weiß das ich viel gefragt habe in letzter Zeit, aber dies ist nun der Abschluss meiner Arbeit :)

würde irgendjemand mir helfen diesen Quellcode von der Klasse R250 zu verstehen?
Kommentieren oder eine Erklärung mit Belegen wäre echt hilfreich... :rolleyes:

Code:
/* R250.java    the r250 uniform random number algorithm, modified for 32 bits

                      (c) Copyright 1994, Everett F. Carter Jr.
                      Permission is granted by the author to use
                  this software for any application provided this
              copyright notice is preserved.

        see:


        Kirkpatrick, S., and E. Stoll, 1981; A Very Fast Shift-Register
            Sequence Random Number Generator, Journal of Computational Physics,
        V. 40.

        Maier, W.L., 1991; A Fast Pseudo Random Number Generator,
        Dr. Dobb's Journal, May, pp. 152 - 157


*/

 

import com.taygeta.rand.RandLCG;

public class R250
{
           private static int BITS = 32;
           private static int MSB = 0x40000000;
           private static int ALL_BITS = 0x7fffffff;
           private static int HALF_RANGE = 0x20000000;
           private static int STEP = 7;

           private int[] r250_buffer = new int[250];
           private int r250_index = 0;
           private int seed_val = 1;
       
           private void init(int sd)
           {
           int j, k;
           int mask, msb;


           RandLCG ran = new RandLCG( sd );
    
           r250_index = 0;
           for(j = 0; j < 250; j++)    /* fill r250 buffer with 15 bit values */
           r250_buffer[j] = ran.rani();

           for (j = 0; j < 250; j++)    /* set some MSBs to 1 */
            if ( ran.rani() > HALF_RANGE )
            r250_buffer[j] |= MSB;


            msb = MSB;      /* turn on diagonal bit */
            mask = ALL_BITS;    /* turn off the leftmost bits */

            for (j = 0; j < BITS; j++)
            {
             k = STEP * j + 3;  /* select a word to operate on */
             r250_buffer[k] &= mask; /* turn off bits left of the diagonal */
             r250_buffer[k] |= msb; /* turn on the diagonal bit */
             mask >>= 1;
             msb  >>= 1;
             }

            }


           public R250() { seed(12331); }

           public R250(int sd) { seed(sd); }

 
           public void seed(int sd)
           {
               seed_val = sd;
           init( seed_val );
       }


           public int rani()    /* returns a random unsigned integer */
           {
             int    j;
             int new_rand;

             if ( r250_index >= 147 )
                  j = r250_index - 147; /* wrap pointer around */
             else
                  j = r250_index + 103;

             new_rand = r250_buffer[ r250_index ] ^ r250_buffer[ j ];
             r250_buffer[ r250_index ] = new_rand;

             if ( r250_index >= 249 )   /* increment pointer for next time */
                    r250_index = 0;
             else
             r250_index++;

            return new_rand;

           }



        public float ranf( )  // 0.0 .. 1.0 base generator for others
        {
            return rani() / (float)( ALL_BITS );
        }


        public float rnumber(float rmin, float rmax)
        {
           float retval = ranf();
           retval = rmin + (rmax - rmin) * retval;
           return retval;
        }

};







und die Klasse RANDLCG die ja nur einen Linearen Kongruenzgenerator darstellt

Code:
// rndlcd.c++   Implementation of Linear Congruential Method
//
//                      (c) Copyright 1994, Everett F. Carter Jr.
//                      Permission is granted by the author to use
//			this software for any application provided this
//			copyright notice is preserved.

 

public class RandLCG
{
        private static int ALL_BITS =    0xffffffff;
	private static int LONG_MAX = 2147483647;
        private static int quotient  = LONG_MAX / 16807;
        private static int remainder = LONG_MAX % 16807;
	
	protected    int seed_val;

	public   RandLCG() { seed(12331); }
	public   RandLCG(int sd) { seed(sd); }


        public int seed(int sd)
        {
                 if ( sd > 0 )
                    seed_val = sd;

                 return seed_val;
       }

       public int rani()       /* returns a random unsigned integer */
       {
	         if ( seed_val <= quotient )
		        seed_val = (seed_val * 16807) % LONG_MAX;
	         else
	         {
		       int high_part = seed_val / quotient;
		       int low_part  = seed_val % quotient;

		       int test = 16807 * low_part - remainder * high_part;

		      if ( test > 0 )
			seed_val = test;
		      else
			seed_val = test + LONG_MAX;

	         }

	         return seed_val; 
          }

          public float ranf()               /* returns a random double in range 0..1 */
          {
	       int new_rand = rani();

	       return new_rand / (float)( ALL_BITS );

          }


};

Vielen Dank :)
 
Zurück