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

  • Home
  • SEARCH
  • 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 4240638
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:11:31+00:00 2026-05-21T03:11:31+00:00

I have written a simple random number generator in C. int l is the

  • 0

I have written a simple random number generator in C. int l is the lower bound and int u is the upper bound.

It works just fine, however I have a question regarding seeding it. If I was to run this in a loop, time(NULL) doesn’t change the seed value quick enough to be able to prevent getting a consecutive series of random numbers that are exactly the same.

I’m wondering how anybody else might have approached this problem. All the examples I’ve found online use time(NULL) as the seed value generator.

int generateRandom(int l, int u)
{
   srand(time(NULL));

   int r = rand() % ((u - l) + 1);
   r = l + r;

   return r;
}

If I was to run these lines of code right next to each other, both Rand1 and Rand2 would be exactly the same.

printf("Rand1 = %d\n", generateRandom(10, 46));
printf("Rand2 = %d\n", generateRandom(10, 46));
  • 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-21T03:11:32+00:00Added an answer on May 21, 2026 at 3:11 am

    srand(time(NULL)) should be run exactly once to intialise the PRNG. Do this in Main when the application starts.

    Explanation:

    A PRNG (Pseudo-Random Number Generator) generates a deterministic sequence of numbers dependent on the algorithm used. A given algorithm will always produce the same sequence from a given starting point (seed). If you don’t explicitly seed the PRNG then it will usually start from the same default seed every time an application is run, resulting in the same sequence of numbers being used.

    To fix this you need to seed the PRNG yourself with a different seed (to give a different sequence) each time the application is run. The usual approach is to use time(NULL) which sets the seed based on the current time. As long as you don’t start two instances of the application within a second of each other, you’ll be guaranteed a different random sequence.

    There’s no need to seed the sequence each time you want a new random number. And I’m not sure about this, but I have the feeling that depending on the PRNG algorithm being used re-seeding for every new number may actually result in lower randomness in the resulting sequence.

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

Sidebar

Related Questions

George Marsaglia has written an excellent random number generator that is extremely fast, simple,
i have written a simple console application just to try boost::thread, i am a
I have written a simple brainfuck interpreter in MATLAB script language. It is fed
I have written the following simple test in trying to learn Castle Windsor's Fluent
I have written a very simple bash script to help me migrate from dev
I have written a secure TCP server in .NET. This was basically as simple
I have big issue with url-rewriting for IIS 7.0. I've written simple module for
I have a simple function written in Oracle 9i (version 9.2.0.4.0) to simulate an
I have a simple little test app written in Flex 3 (MXML and some
I have a simple GUI component written in Java. The class draws an analog

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.