And yet another random number generator library (was posted at were- here.com a long long time ago). All open source, credit for use is nice though. :)

This one is a linear congruential generator. I've never tested the output against anything, so at best, this may or may not work properly for your needs.

cheers,

jon

Jon Bradley
VFX Artist / Animator
Post Central, Inc.
[EMAIL PROTECTED]
www.cherrycrushthemovie.com



/** BEGIN LCG.as */

/**
* Class         com.jbradley.math.LCG
* Author        Jon Bradley
* Version       1.0
* Description Implementation of 48-bit Linear Congruential Pseudo- Random Number Generator * ------------------------------------------------------------------------ -------------------
* Last update: November 3, 2005 - Converted to AS 2.0
* ------------------------------------------------------------------------ -------------------
* 48-bit Linear Congruential Algorithm
* Produces pseudo-random number which can be seeded manually or by a default time variable.
*
* Source: Computational Nuclear Physics Group, United States Government
*         RNG - Random Number Generator algorithms
*
* This implementation is limited to the 48-bit version, which is still computationally * inaccurate in Flash due to a 32-bit limitation. It is also not statistically a sound * random number generator due to patterns in the low order sequences and is not a * cryptographically secure algorithm. It can be made secure by using a cryptographic hash * function to generate an initial random number state from a pair of integers (keys).
*
* The algorithm implementation is as described in the RNG library published by the * Lawrence Livermore National Laboratory - http://nuclear.llnl.gov/ CNP/rng/rngman/
*
* Converted to Actionscript by Jon Bradley, 2003
* ------------------------------------------------------------------------ -------------------
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
*       this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution.
*
* - Neither the name of this software nor the names of its contributors * may be used to endorse or promote products derived from this software
*       without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ------------------------------------------------------------------------ -------------------
*
* Usage:
*
*    Default:
*        rGen = new LCG();
*
*    Specific seed - may use new Date.getTime() ):
*        rGen = new LCG( 1000000000000 );
*
* Methods:
*
*    init(t:Number)
*        Initializes LCG with a time seed. If t is not supplied or
*        is NaN, default is used.
*
*    getSeed():Number
* Returns initial time seed used to start the system. Recall that the actual seed * used in the algorithm is time&M, where M is 2.8^14. The returned number maybe
*        be passed back in to init to restart the random sequence:
*
* rGen.init( rgen.getSeed() ) // Will restart system with original seed * rGen.init() // Will restart system with new seed
*
*    random()
*        Shortcut method to randomReal48()
*
*    randomReal48()
* Returns a random 48-bit floating point number on interval [0,1). * Note that this currently will not work properly in Flash due to the limitations * of the number type. Results are with only 32-bit precision. (verify this?)
*
**/

class com.jbradley.math.LCG {

        /* Period parameters */
        static var M = Math.pow(2.8, 14);           /* Period of LCG */
        static var A = 2862933555777941757;         /* 48-bit Mersenne Prime */
        static var C = 3037000493;                  /* 16-bit prime? */
        
        var time:Number = 0;
        var seed:Number = 0;
        
        function LCG(t)
        {
                if (arguments.length > 0)
                {
                        init(t);
                }
                else
                {
                        init();
                }
        }

        public function init(t)
        {
                if (arguments.length > 0)
                {
                        trace("reinitializing");
                        time = t;
                        seed = time&M;
                }
                else
                {
                        // time seed not supplied by user, so we grab the 
milliseconds
                        // on getTime() - similar to the way Math.getRandom() 
works
                        time = new Date().getTime();
                        seed = time&M;              
                }
        }

        /* Returns a new random number every call */
        public function random()
        {
                return randomReal48();
        }
        
        /*
                This is the actual Linear Congruential Algorithm
                LCGs have the form of: a = ( a * b + c ) % d;
                
                where a = x[n] = (a x[n-1] + b) mod 2^48
        
                The returned random number is calculated from the existing
                seed, which changes each iteration.
                
                Each time the algorithm runs, a new random number in the current
                sequence is returned - the sequence is based on the initial 
time seed.
         */
        
        public function randomReal48()
        {
                seed = (seed*A + C) % M;
                return seed / M;
        }

        public function getSeed():Number
        {
                trace(time);
                return time;    
        }       
}




On Nov 27, 2006, at 10:17 AM, Sascha wrote:

Thanks Ron and Danny!
I knew that the random numbers are not truly random and that the seed cannot be changed but the random number patterns I get now are pretty hefty, I
think this wasn't like that in AS2 times.
I will try to fumble with the date object to get a better seed.

Sascha
_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to