C# Mersenne Twister random integer generator implementation (SFMT) monte carlo simulation

后端 未结 5 1577
梦毁少年i
梦毁少年i 2021-02-01 10:37

So far I\'ve been using the C# Mersenne Twister found here to generate random numbers:

http://www.centerspace.net/resources.php

I just discovered SFMT

相关标签:
5条回答
  • 2021-02-01 11:01

    What you can do is download the source from the link you discovered on Code Project. Unzip it, load the solution in Visual Studio and compile it. This will give you source, an unmanaged c dll and a .lib file.

    You can P/Invoke the functions in this dll, (there are only 5 simple functions exported, of which you need only two) or you can use this dll, lib, and the SFMT header file to create a managed wrapper dll you can use in C# without P/Invoke. I just tried this method and it was very simple to do. There was no explicit marshalling involved.

    Here's how. Once you have downloaded and compiled the source (you need the header and the lib file that is created in addition to the dll) create a new C++ CLR Class Library project. Call it WrapSFMT or something. Go the project properties. Under C++/Precompiled Headers, change to "Not using precompiled headers." Under the Linker/General/Additional Library Directories, enter the path to the SFMT.lib. Under Linker/Input/Additional Dependencies, add SFMT.lib. Close the property pages. Copy SFMT.h to your project folder and include it in the project.

    Edit WrapSFMT.h to read as follows:

    #pragma once
    #include "SFMT.H"
    
    using namespace System;
    
    namespace WrapSFMT {
    
    public ref class SRandom
    {
    public:SRandom(UInt32);
    public:UInt32 Rand32(void);
    };
    }
    

    These declare the methods that will be in your class. Now edit WrapSFMT.cpp to read:

    #include "WrapSFMT.h"
    
    namespace WrapSFMT {
    
    SRandom::SRandom(UInt32 seed)
    {
        init_gen_rand(seed);
    }
    
    UInt32 SRandom::Rand32()
    {
        return gen_rand32();
    }
    }
    

    These implement the methods you declared in the header file. All you are doing is calling functions from the SFMT.dll, and C++/CLI is automatically handling the conversion from unmanaged to managed. Now you should be able to build the WrapSFMT.dll and reference it in your C# project. Make sure the SFMT.dll is in the path, and you should have no problems.

    0 讨论(0)
  • 2021-02-01 11:08

    Maybe this is what you're looking for? There is a list of several implementations.

    Specifically, this one (by Cory Nelson) might be useful.

    0 讨论(0)
  • 2021-02-01 11:13

    I don't really see your problem with speed here. On my machine (Core 2 Duo T7200 @ 2 GHz) generating a random integer with MT19937 or MT19937-64 takes around 20 ns (on average, when drawing 50000 numbers). So that'd be around 4,32 × 1012 (so around 4 trillion numbers) a day. And that's for one core. With Java. So I think you can expect the performance to be more than adequate for your needs.

    To actually answer your question: I don't know of a C# implementation of SFMT, but conversion of the C code to C# should be fairly straightforward. However, you're not gaining much, as SFMT is optimized for SIMD and C# currently doesn't support this directly.

    0 讨论(0)
  • 2021-02-01 11:17

    Is there a reason you can't compile the C implementation into a DLL and call this from your C# code?

    EDIT:

    I'm sorry, but I have only a very limited knowledge of C (and indeed C#), but the "How to create a C dll" may be answered here: http://www.kapilik.com/2007/09/17/how-to-create-a-simple-win32-dll-using-visual-c-2005/ and the how fast can be checked by profiling the code.

    0 讨论(0)
  • 2021-02-01 11:20

    You can find a C# implementation of SFMT (plus other RNG algorithms) at... http://rei.to/random.html The page and source code comments are in Japanese but you should be able to figure it out.

    You can also find a Google-translated version (to English) of the page at... http://translate.google.com/translate?hl=en&sl=ja&u=http://rei.to/random.html

    0 讨论(0)
提交回复
热议问题