Thread: Does it guarantee that random numbers generated are different with each other.

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    26

    Does it guarantee that random numbers generated are different with each other.

    Dear all,

    I have one question about generating random number. My snippet of code is as follows,

    Code:
    srand(0);
    
    for (i = 0; i< 10,000,000; i++) 
    {
        random_numbers[i] = rand();
    }
    My case requirement is that each value in the set of generating random number should be different.

    However, I am not sure whether or not even the upper snippet code will acheive this goal?

    Any suggestion, hint?

    Thanks in advance,

  2. #2
    Registered User
    Join Date
    Nov 2006
    Location
    Coimbra, Portugal
    Posts
    64
    Quote Originally Posted by qingxing2005
    srand(0);
    Don't do this when generating random numbers. The srand parameter is called a "seed". If you want a different sequence of numbers every time you run your program, use

    Code:
    srand((unsigned)time(0));
    time() is defined in time.h, by the way

    As for your question, your snippet does not guarantee that the generated numbers will be different. In fact, generating 10000000 values doesn't help either. With rand(), you can only generate numbers between 0 and RAND_MAX, where RAND_MAX is 32767 for most compilers. My suggestion would be to use a vector, but there are no vectors in C, so perhaps you should give it a go on implementing one yourself or wait for other suggestions.
    Last edited by Mr_Miguel; 12-13-2006 at 12:50 PM.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    some compilers use RAND_MAX value == 32000 (don't remember the exact value, but close)
    so on this compilers you will never receive 10000000 different values

    also, even if you get some rand() fuction that provides a range you need it will never garantee the uniquness of the generated values...

    So if you want unique numbers use some other approches...

    look forum post for shuffle function - you will find some samples that might be helpful
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    some compilers use RAND_MAX value == 32000 (don't remember the exact value, but close)
    Probably 2^15-1, 32767, which is also SHRT_MAX on most compilers. (Mr_Miguel already mentioned this.)

    Though in fact most compilers use INT_MAX for RAND_MAX, so it might be 2^31-1. But that's not a good thing to rely on.

    Code:
    for (i = 0; i< 10,000,000; i++)
    Putting commas in a number doesn't do what you want. Leave out the commas.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    Quote Originally Posted by dwks
    Probably 2^15-1, 32767, which is also SHRT_MAX on most compilers. (Mr_Miguel already mentioned this.)

    Though in fact most compilers use INT_MAX for RAND_MAX,
    MSVC6 and MSVC++2005 Expert use 32767
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Dev-C++, GCC (many versions, I have 4 versions which all do), and many other compilers use 2^31-1.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Use the Mersenne Twister generator for a guaranteed larger range.Then generate an array of integers from 1 to X, and perform a Knuth shuffle with the rand generator on the array. This guarantees a random permutation of X unique numbers.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  8. #8
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Ume, Sweden
    Posts
    555
    Quote Originally Posted by dwks
    Dev-C++, GCC (many versions, I have 4 versions which all do), and many other compilers use 2^31-1.
    Strange, Dev-C++ isn't using vanilla MinGW? MinGW has always had
    #define RAND_MAX 0x7FFF
    in stdlib.h on my computer.

  9. #9
    .
    Join Date
    Nov 2003
    Posts
    307
    non-repeating "random" numbers are not random. Random means that there is an equal chance for each of all possible numbers in the range. In other words, if you get a 7, the next time you generate a number, 7 should still be one of the possiblilities, it should not be excluded. Random numbers can repeat.

    What you seem to want is analogous to dealing cards from a shuffled deck. You probably want a large array that is first "shuffled" and then "dealt".

  10. #10
    Registered User
    Join Date
    Dec 2006
    Posts
    26
    Quote Originally Posted by dwks
    Probably 2^15-1, 32767, which is also SHRT_MAX on most compilers. (Mr_Miguel already mentioned this.)

    Though in fact most compilers use INT_MAX for RAND_MAX, so it might be 2^31-1. But that's not a good thing to rely on.

    Code:
    for (i = 0; i< 10000000; i++)
    Putting commas in a number doesn't do what you want. Leave out the commas.
    Hey, in my piece of code, there ie only 10000000 used. I donnot so whether this number appeared. :0)

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    RAND_MAX is a maxmimum number returned by the rand()

    And because rand() returns numbers from the interval [0,RANDMAX] it also describes the number of different values that can be received from this function
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    > [0,RANDMAX]
    Umm . . . perhaps you mean (0,RANDMAX]. Or [0,RANDMAX). I can never remember which.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    The rand function returns a pseudorandom integer in the range 0 to RAND_MAX
    both 0 and RAND_MAX are possible return values for rand()
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You're right, sorry.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Question about random numbers
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 07-02-2008, 06:28 AM
  3. Generatin Random Numbers
    By dantu1985 in forum C Programming
    Replies: 15
    Last Post: 08-13-2007, 01:21 AM
  4. Random numbers
    By Mavix in forum C Programming
    Replies: 3
    Last Post: 05-13-2007, 09:01 AM
  5. Generating 100k to 1 million unique random numbers
    By Ariod in forum C Programming
    Replies: 4
    Last Post: 08-26-2005, 12:59 PM