Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8871549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:01:01+00:00 2026-06-14T18:01:01+00:00

I have written a function in C++ to let me take advantage of the

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:01:02+00:00Added an answer on June 14, 2026 at 6:01 pm

    Certainly, getting 200 random numbers from a single call will be faster than getting 200 random numbers from 200 different calls. It might even be many times faster. But it’s likely that you’re talking a difference of milliseconds. So it might not be worth doing. Will the difference of a few milliseconds make a noticeable difference to the overall performance of your application?

    If you do decide to do it, you probably don’t want to mess with vector, but rather with UINT32[]. Marshaling a vector between C# and C++ would be difficult at best. For all practical purposes, impossible.

    See Marshaling Different Types of Arrays for examples of how to marshal arrays.

    You’ll probably want to allocate the array in C# and pass it along with the size to the C++ function. That way, you don’t have to worry about deallocating the memory. If you have the C++ code allocate the array and return it, then the C# code will have to call a C++ function to deallocate the memory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the simple function let OutputDebugToConsole () = new System.Diagnostics.TextWriterTraceListener(System.Console.Out) |> System.Diagnostics.Debug.Listeners.Add |>
I have written a function like this let GetAllDirectAssignmentsforLists (spWeb : SPWeb) = spWeb.Lists
I have written this small function to retrieve the tail of a list: let
I have written a function that sorts a big scale of data. To test
I have written a function that extract the domain from hostname. e.g. www.domain.com ->
I have written this function to auto correct gender to M or F from
I have written a function which warn a user before refreshing the (I require
I have written a function that positions a tooltip just above a textbox. The
I have written a function below: void trans(double x,double y,double theta,double m,double n) {
I have written a function for a multilevel wordpress menu, but I'd like it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.