I have written a function in C++ to let me take advantage of the new Intel RdRand digital random number generator via an intrinsic function.
__declspec(dllexport) int __stdcall GetRdRand32(PUINT32 pValue)
{
return _rdrand32_step(pValue);
}
I have wrapped it so that I can use it in C# via PInvoke and it is working fine as follows:
[DllImport("CppDynamicLinkLibrary.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int GetRdRand32(out UInt32 str);
My use case could often involve requesting more than one random number although probably only on the order of hundreds at a time (per requester). My question is, as I'm using C++ anyway, would it make sense to put together another function that can return a dynamic array (or vector) of random numbers, i.e. would this greatly improve performance over just making multiple calls to the C++ DLL? Performance is a concern because this will be on a server application that could be sending ~200 random numbers to many clients at similar times
If it is worthwhile doing, how would I go about doing it? I was thinking something along the lines of the following, although my guess is finding a way to get the vector into C# could easily be a performance concern?
__declspec(dllexport) void __stdcall vGetRdRand32(std::vector<UINT32> &pArray)
{
for (std::vector<UINT32>::iterator It = pArray.begin(); It != pArray.end(); It++ )
_rdrand32_step(&(*It));
}
Finally, would Marshal.Copy be better than the latter approach, could anyone point me in right direction if it would be?