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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:20:29+00:00 2026-05-18T08:20:29+00:00

How can I generate different random numbers at runtime? I’ve tried srand((unsigned) time(0)); But

  • 0

How can I generate different random numbers at runtime?

I’ve tried

srand((unsigned) time(0));

But it seems to get me a random number on every startup of the program, but not on every execution of the function itself…

I’m trying to automate some tests with random numbers, random iterations, number of elements, etc… I thought I could just call

srand((unsigned) time(0));

at the beginning of my test function and bingo, but apparently not.

What would you suggest me to do?

  • 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-05-18T08:20:30+00:00Added an answer on May 18, 2026 at 8:20 am

    srand()

    As others have mentioned. srand() seeds the random number generator. This basically means it sets the start point for the sequence of random numbers. Therefore in a real application you want to call it once (usually the first thing you do in main (just after setting the locale)).

    int main()
    {
        srand(time(0));
    
        // STUFF
    }
    

    Now when you need a random number just call rand().

    Unit Tests

    Moving to unit testing. In this situation you don;t really want random numbers. Non deterministic unit tests are a waste of time. If one fails how do you re-produce the result so you can fix it?

    You can still use rand() in the unit tests. But you should initialize it (with srand()) so that the unit tests ALWAYS get the same values when rand() is called. So the test setup should call srand(0) before each test (Or some constant other than 0).

    The reason you need to call it before each test, is so that when you call the unit test framework to run just one test (or one set of tests) they still use the same random numbers.

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

Sidebar

Related Questions

I am trying to generate a different random number every time my RandomNumber method
How I can generate different random numbers in one moment in c++? Now I
I am trying to generate 5 different random numbers for use in my card
In F#, you can generate a set of numbers, just by saying [1..100]. I
How can I generate a (pseudo)random alpha-numeric string, something like: 'd79jd8c' in PHP?
How can i generate bytecode (Byte[]) from a String at runtime, without using a
always when I try to generate a random number with Int = arc4random %
This is the first time I'm trying random numbers with C (I miss C#).
I need to generate a random number, now this number needs to be somewhere
I want to create 3 random number at a time (simultaneously). However, they returned

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.