MSVC++ rand()和C#System.Random之间的关系

bob*_*obo 0 c# c++ random

我怎样才能使MSVC++程序和C#.NET程序产生相同的随机数序列?

可能吗?MSVC++ rand()System.Random?之间有什么关系吗?

鉴于下面的例子,它们似乎完全不同.

#include <iostream>
using namespace std;

int main()
{
  srand( 1 ) ;

  cout << rand() << endl <<
    rand() << endl <<
    rand() << endl ;
}
Run Code Online (Sandbox Code Playgroud)
using System;

class Program
{
  static void Main( string[] args )
  {
    Random random = new Random( 1 );

    Console.WriteLine( random.Next() );
    Console.WriteLine( random.Next() );
    Console.WriteLine( random.Next() );
  }
}
Run Code Online (Sandbox Code Playgroud)

Ste*_*Mai 5

最简单的方法是对包含随机数生成器的C++ DLL进行非托管引用,并将参数作为种子.然后你可以肯定他们是一样的.

  • 这是正确的方法,而不是接受的答案.仅为随机函数引入CLR将是疯狂的. (2认同)