C#Mersenne Twister随机整数生成器实现(SFMT)蒙特卡洛仿真 [英] C# Mersenne Twister random integer generator implementation (SFMT) monte carlo simulation

查看:216
本文介绍了C#Mersenne Twister随机整数生成器实现(SFMT)蒙特卡洛仿真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我一直在使用C# Mersenne Twister 来生成随机数:

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

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

我刚发现 SFMT 速度快一倍:

I just discovered SFMT which is supposed to be twice as fast here:

http://www.math. sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/

谁能指出我的SFMT C#实现?

我的要求是生成介于(包括)0到2 ^ 20(1048576)之间的整数.

My requirements are to generate an integer between (and including) 0 and 2^20 (1048576).

为了每天24小时运行一次仿真,我需要每天进行万亿次,因此我准备花几天的时间来完善它.

I need to do this trillions of times everyday for a simulation running on a 24 hour clock so I am prepared to spend days tweaking this to perfection.

目前,我已经通过添加新方法来调整中心空间梅森·扭曲者(Mersenne Twister)来满足自己的要求:

Currently I've tweaked the Center Space Mersenne Twister by adding a new method to fit my requirements:

public uint Next20()
{            
    return (uint)(genrand_int32() >> 12);
}

使用方法genrand_int32(),我想生成自己的版本genrand_int20(),该版本会生成一个0(和包括)0到2 ^ 20之间的整数,以保存在上面的上并进行移位,但我不理解数学.究竟该怎么办?

Using the method genrand_int32() I'd like to produce my own version, genrand_int20(), that generates an integer between (and including) 0 and 2^20 to save on the cast above and shift but I don't understand the mathematics. Exactly how can I do this?

也正在使用 uint会比int更快,还是仅仅是可寻址数字的问题?因为我只需要1048576,所以我只关心速度.

Also is using an uint going to be faster that int, or is just a matter of addressable numbers? Because I only need up to 1048576, I am only concerned with speed.

它还将在具有.NET 2的 Windows Server 2003 R2 SP2(32位)盒上运行.处理器为

Also this will be running on a Windows Server 2003 R2 SP2 (32bit) box with .NET 2. Processor is AMD Opteron 275 (4 core).

推荐答案

您可以下载来自您在Code Project上发现的链接的来源.解压缩,在Visual Studio中加载解决方案并进行编译.这将为您提供源代码,非托管的C dll和.lib文件.

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.

您可以P/调用该dll中的功能(仅导出5个简单功能,其中仅需要两个),也可以使用此dll,lib和SFMT头文件来创建托管包装dll.您可以在不使用P/Invoke的C#中使用.我只是尝试了这种方法,所以非常简单.没有明确的编组.

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.

这是怎么回事.下载并编译 (您需要除dll之外还需要创建的标头和lib文件)之后,创建一个新的C ++ CLR类库项目.称之为WrapSFMT或其他名称.转到项目属性.在"C ++/预编译头"下,更改为不使用预编译头".在链接器/常规/附加库目录下,输入SFMT.lib的路径.在链接器/输入/其他依赖项下,添加SFMT.lib.关闭属性页.将SFMT.h复制到您的项目文件夹中,并将其包含在项目中.

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.

编辑WrapSFMT.h内容如下:

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);
};
}

这些声明将在您的类中使用的方法.现在,将WrapSFMT.cpp编辑为:

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();
}
}

这些实现您在头文件中声明的方法.您要做的只是从SFMT.dll调用函数,C ++/CLI自动处理从非托管到托管的转换.现在,您应该能够生成WrapSFMT.dll并在您的C#项目中引用它.确保SFMT.dll在路径中,并且应该没有问题.

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.

这篇关于C#Mersenne Twister随机整数生成器实现(SFMT)蒙特卡洛仿真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆