乱数とDLLと
乱数を生成するCプログラムがあった。メルセンヌツイスターとかいうの。
これをDLL化してみようと思いつく。
そしてこれをC#で使ってみようと思いつく。
詳細は、上記のリンクに任せます。
どうやら下たみたいな感じで関数宣言して(すべての関数宣言にextern "C"とWINAPIを追加)、
extern "C" void WINAPI init_genrand(unsigned long s)
defファイルを
LIBRARY mt19937ar ;エクスポートする関数 EXPORTS init_genrand init_by_array genrand_int32 genrand_int31 genrand_real1 genrand_real2 genrand_real3 genrand_res53
こんな風に作ってリンカに登録すれば良いらしい。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace MT { class Program { static void Main(string[] args) { init_by_array(4,5,4); for (var i = 0; i < 16; i++) { Console.WriteLine(genrand_int32()); } Console.ReadKey(); } const string mt19937ar = "mt19937ar.dll"; [DllImport(mt19937ar)] public static extern void init_genrand(int seed); [DllImport(mt19937ar)] static extern void init_by_array(uint[] init_key, int key_length); public static void init_by_array(params uint[] init_key) { init_by_array(init_key, init_key.Length); } [DllImport(mt19937ar)] public static extern int genrand_int32(); [DllImport(mt19937ar)] public static extern double genrand_real1(); [DllImport(mt19937ar)] public static extern double genrand_real2(); [DllImport(mt19937ar)] public static extern double genrand_real3(); } }