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 8793767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:09:54+00:00 2026-06-13T23:09:54+00:00

So, I am making a DLL that I want to have a function to

  • 0

So, I am making a DLL that I want to have a function to generate random numbers. I was wondering which of these options is more efficient (performance wise).

This one is just making a function in a DLL that allows me to get a random number.

int getRand(unsigned int seed) {
    int rNum;  // Random Number.

    srand(seed);
    rNum = (rand() % // Whatever I need here.
}

Or, would just using srand(time(nullptr)) and rand() in the application be better in performance?

Thanks,

Johnny P.

  • 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-13T23:09:55+00:00Added an answer on June 13, 2026 at 11:09 pm

    There isn’t really any need to create a function that combines a call to srand() with one to rand().

    srand() is used to seed (i.e. to initialise) a sequence of random numbers. Subsequent, and repeated, calls to rand() are then used to actually generate the random numbers. (rand() updates the internal state of the random number generator, so it (generally) won’t generate the same number again when called the next time.)

    If you call srand() each time before you call rand(), the sequence will be re-initialised each time. If the seed passed is the same each time, your function will return the same random number each time.

    Therefore, the two calls will have to be separated anyway.

    You may also be interested in the random number generation framework provided by the <random> header in C++11. Here is example code (mostly copied from cppreference):

    #include <random>
    #include <iostream>
    
    int main()
    {
      /* Initialise and seed. Do this only once. */
      std::random_device rd;
      std::mt19937 gen(rd());
    
      /* Define how to use the raw random numbers, i.e.
         a probability distribution. In this case a
         uniform distribution with six possible integer
         events. */
      std::uniform_int_distribution<> dis(1, 6);
    
      /* Generate a series of random events. No reseeding. */
      for(int n=0; n<10; ++n)
          std::cout << dis(gen) << ' ';
    
      std::cout << std::endl;
    }
    

    cppreference has a pretty good description of the <random> header and the many options it provides: http://en.cppreference.com/w/cpp/numeric/random.

    Most of this is inspired by the Boost random library, which provides even more options: http://www.boost.org/doc/libs/1_52_0/doc/html/boost_random.html (Link to the 1.52.0 version). Therefore, if you can’t use C++11, or you need functions not provided by the Standard, using the Boost library may be a good idea.

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

Sidebar

Related Questions

Making an adobe flex ui in which data that is calculated must use proprietary
I have a bit of code for a dll that is needed by two
I'm making a task-based program that needs to have plugins. Tasks need to have
I have a windows DLL that currently only supports ASCII and I need to
I'm making an unmanaged C++ DLL which uses the C# managed DLL. I'm writting
I don't want to use WebKit .NET because it doesn't have some functionality that
How can I have code-sharing between two projects without making a dll? The issue
What I want to have happen is that the console window just goes away,
Everything I have read says that when making a managed stored procedure, to right
I have a VB6 Active DLL that I'm trying to call from C#. I've

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.