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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:13:41+00:00 2026-06-11T06:13:41+00:00

I am using rand() for a 6 digit field which needs unique values. Am

  • 0

I am using rand() for a 6 digit field which needs unique values. Am I doing it right?

What are the odds, rand() can give me similar values on consecutive or frequent calls?

It was unique when I used rand(). But, returned same number when I called srand(time(NULL)) or srand(clock()). Seems, like it’s working opposite for me. Or is it?

  • 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-11T06:13:42+00:00Added an answer on June 11, 2026 at 6:13 am

    As others have pointed out, uniqueness is not guaranteed. However you are probably seeing repeated numbers because you are using srand() and rand() incorrectly.

    srand() is used to seed the random number generator. that means a series of calls to rand() after a call to srand will produce a particular series of values. If you call srand() with the same value then rand() will produce the same series of values (for a given implementation, there’s no guarantee between different implementations)

    int main() {
        srand(100);
        for(int i = 0; i<5; ++i)
            printf("%d\n",rand());
    
        printf("\nreset\n\n");
    
        srand(100);
        for(int i = 0; i<5; ++i)
            printf("%d\n",rand());
    
    }
    

    for me this produces:

    365
    1216
    5415
    16704
    24504
    
    reset
    
    365
    1216
    5415
    16704
    24504
    

    time() and clock() return the time, but if you call them quickly enough then the value returned will be the same, so you will get the same series of values out of rand().

    Additionally rand() is generally not a very good random number generator and using it usually means you have to transform the series of numbers to the distribution you actually need. You should find a different source of randomness and either learn the proper ways to produce the distribution you want or use a library that can do it for you. (for example one common method of producing a ‘random’ number between 0 and N is to do rand() % N but this is not really the best method.

    C++ provides a much better random number library in <random>. It provides different PRNG algorithms, such as linear_congruential, mersennne_twister, and possibly even a cryptographically secure RNG (depending on the implementation). It also provides objects for producing a variety of distributions, such as uniform_int_distribution which should avoid the mistake make in rand() % N.

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

Sidebar

Related Questions

I need a random 4 digit number right now im using rand(1000,9999) that always
I've got an unmanaged c++ console application in which I'm using srand() and rand().
i was told to write program which generate unique 5 digit number ( for
I know that just using rand() is predictable, if you know what you're doing,
I'm trying to generate a bunch of random numbers using rand() % (range). Here's
I want to get randomized results for a query. I read that using RAND()
Take the following program: #include <cstdlib> using std::rand; #include <iostream> using std::cout; int main()
If I create a cell array using: clear all data = {rand(1,5),rand(1,4),rand(1,4),rand(1,6)}; a =
I've created a salt using; md5(rand(0,10000000)); (there is probably a better way?) There doesn't
I'm using this to get random char from the string pool: $charset=str_shuffle('./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'); $charsetLength=strlen($charset)-1; $chosen=$charset[rand(0,$charsetLength)];

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.