Click here to Skip to main content
15,892,737 members
Articles / Programming Languages / C#

SFMT in Action: Part II – Object oriented implementation of SIMD-oriented Fast Mersenne Twister (SFMT) in C#

Rate me:
Please Sign up or sign in to vote.
4.71/5 (5 votes)
31 Oct 2009CPOL18 min read 47K   620   9  
The new SIMD-oriented Fast Mersenne Twister (SFMT) library was developed using object oriented technologies such as UML, composition, aggregation, generalization, multiplicity, and Design Patterns.
/****************************** Module Header ******************************\
* Module Name :	SfmtC.cs
* Project     :	PrngLib
* Author      : Emre Özgür İnce
* Date        : 08/23/2009
*
* All rights reserved.
\***************************************************************************/

using System;

namespace PrngLib
{
    public class SfmtC : SfmtStrategy
    {

        #region Delegate Functions
        delegate void init_gen_rand(uint seed);
        delegate uint gen_rand32();
        delegate ulong gen_rand64();
        delegate int fill_array32(int[] array, int size);
        delegate int fill_arrayU32(uint[] array, int size);
        delegate int fill_array64(long[] array, int size);
        delegate int fill_arrayU64(ulong[] array, int size);
        #endregion

        #region Fields
        init_gen_rand _initGenRand = null;
        gen_rand32 _genRand32 = null;
        gen_rand64 _genRand64 = null;
        fill_array32 _fillArray32 = null;
        fill_arrayU32 _fillArrayU32 = null;
        fill_array64 _fillArray64 = null;
        fill_arrayU64 _fillArrayU64 = null;
        #endregion

        #region Constructors
        public SfmtC()
        {
            _dllName = "SFMTc";
            _unmanagedLibrary = new UnmanagedLibrary(_dllName);
            _initGenRand = _unmanagedLibrary.GetUnmanagedFunction<init_gen_rand>("init_gen_rand");
            _genRand32 = _unmanagedLibrary.GetUnmanagedFunction<gen_rand32>("gen_rand32");
            _genRand64 = _unmanagedLibrary.GetUnmanagedFunction<gen_rand64>("gen_rand64");
            _fillArray32 = _unmanagedLibrary.GetUnmanagedFunction<fill_array32>("fill_array32");
            _fillArrayU32 = _unmanagedLibrary.GetUnmanagedFunction<fill_arrayU32>("fill_array32");
            _fillArray64 = _unmanagedLibrary.GetUnmanagedFunction<fill_array64>("fill_array64");
            _fillArrayU64 = _unmanagedLibrary.GetUnmanagedFunction<fill_arrayU64>("fill_array64");

        }
        #endregion

        #region Properties
        public override string DllName
        {
            get { return _dllName; }
        }
        #endregion

        #region Methods
        public override void MakeSeed()
        {
            _initGenRand((uint)DateTime.Now.Millisecond);
        }
        
        public override int Generate32BitInt()
        {
            return (int)_genRand32();
        }

        public override bool Generate32BitInt(int[] intArray)
        {
            bool result = false;
            int r = _fillArray32(intArray, intArray.Length); 
            if (r == 1)
                result = true;
            return result;

        }

        public override bool Generate32BitUInt(uint[] intArray)
        {
            bool result = false;
            int r = _fillArrayU32(intArray, intArray.Length);
            if (r == 1)
                result = true;
            return result;

        }

        public override uint Generate32BitUInt()
        {
            return _genRand32();
        }

        public override bool Generate64BitInt(long[] longArray)
        {
            bool result = false;
            int r = _fillArray64(longArray, longArray.Length);
            if  (r == 1)
                result = true;
            return result;
        }

        public override bool Generate64BitUInt(ulong[] longArray)
        {
            bool result = false;
            int r = _fillArrayU64(longArray, longArray.Length);
            if (r == 1)
                result = true;
            return result;
        }

        public override long Generate64BitInt()
        {
            return (long)_genRand64();
        }

        public override ulong Generate64BitUInt()
        {
            return _genRand64();
        }
        #endregion

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Parola Bilgi Teknolojileri Ltd.
Turkey Turkey
He was born in Bursa, Turkey in 1979, and still lives in this fascinating place.

He started programming with Fortran when he was at university and then has experienced with MS Visual Basic, Borland Delphi, Java, C++ and C#.

He developed database based several client-server and multi-tier applications using OO concepts and OOP. He worked with Oracle, MySQL, MSSQL and Firebird RDBMS.

In the past, he worked on freetime image processing projects using OpenGL, DirectX, SDL technologies. He's interested in encryption/decryption and compression/decompression algorithms, random number generators and computational algorithms such as Monte Carlo Methods.

In 2011, he founded Parola Information Technology Ltd.

http://www.markanabak.com.tr is serviced by Parola Information Technology Ltd. It's a SAAS model for Intellectual Property sector.

Comments and Discussions