CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Aug 2000
    Posts
    1,471

    Help with generating random number

    If I want to generate 10 random numbers ranging from 0 to 9, is it possible to have 10 different numbers every time? For example, every time they should be like this, 1,3,4,9,0,2,8,7,5,6 without duplicate. I tried srand and rand but it looks like it doesn't work. They simply will generate duplicate numbers. Thanks for your inputs.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Help with generating random number

    Yes, by shuffling a container of those numbers.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Help with generating random number

    http://www.cplusplus.com/reference/a...andom_shuffle/

    You'll even notice that the example is your problem (minus the 0).
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  4. #4
    Join Date
    Aug 2000
    Posts
    1,471

    Re: Help with generating random number

    Thanks for your inputs. I guess you meant random_shuffle. But random_shuffle generates the same result every time. I wonder if it is possible to generate different result every time by using random_shuffle. It looks like it doesn't accept seed. Thanks for your inputs.
    Quote Originally Posted by laserlight View Post
    Yes, by shuffling a container of those numbers.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Help with generating random number

    Quote Originally Posted by dullboy View Post
    Thanks for your inputs. I guess you meant random_shuffle. But random_shuffle generates the same result every time. I wonder if it is possible to generate different result every time by using random_shuffle. It looks like it doesn't accept seed. Thanks for your inputs.
    I tried the example in the link above. It generates different numbers for me.

  6. #6
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Help with generating random number

    Quote Originally Posted by GCDEF View Post
    I tried the example in the link above. It generates different numbers for me.
    unfortunately, random_shuffle is not guaranteed neither to seed itself nor to use rand internally, therefore using srand() will have no effect in general. So, you should specify your own random number generator in the random_shuffle call.

    EDIT: oh sorry, I didn't realized that the example in the link already covered the user provided random generator case ...
    Last edited by superbonzo; August 27th, 2010 at 08:51 AM.

  7. #7
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: Help with generating random number

    Computers cannot generate random number , because behind each rand() algorithm there a sequence , for further reading pls. see this

    http://en.wikipedia.org/wiki/Random_number_generation

  8. #8
    Join Date
    Aug 2000
    Posts
    1,471

    Re: Help with generating random number

    Quote Originally Posted by monarch_dodra View Post
    http://www.cplusplus.com/reference/a...andom_shuffle/

    You'll even notice that the example is your problem (minus the 0).
    That is a good example. How does p_myrandom work in the example? It looks like p_myrandom is a function pointer applied to each element of the vector. But how'd I understand i in the definition of myrandom?Thanks for your inputs.

  9. #9
    Join Date
    Nov 2008
    Location
    North America
    Posts
    37

    Re: Help with generating random number

    You could try this:
    Code:
    srand(time(0));
    
    int a[10] = {0};
    int n;
    bool duplicate;
    bool loop;
    
    for(int i = 0; i <= 9; ++i)
    {
        loop = true;
        while(loop)
        {
            n = 1+rand()%10;
            duplicate = false;
            for(int j = 0; j <= 9; ++j)
            {
                if(a[j] == n)
                    duplicate = true;
            }
            if(duplicate == false)
            {
                a[i] = n;
                loop = false;
            }
        }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured