Random numbers
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Random numbers

  1. #1
    Join Date
    Sep 2012
    Posts
    10

    Random numbers

    I want to have the random numbers between 1 to 5 for a variable of 5arrays. for having the random numbers i am using the code as below.
    Code:
    for(int i=1;i<=5;i++)
        { int j=rand()%5;
                cout<<j<<endl;
                }
    The problem with this way of generating random number is that i get the same set of random numbers in every outputs. Is there any other way to have different random numbers with many different outcomes as much as possible.

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

    Re: Random numbers

    You probably forgot to use srand to seed the PRNG.

    Also, if the numbers must be different on each iteration, then you would be better off creating an array of integers from 1 to 5 then shuffling the array with std::random_shuffle.
    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
    Jan 2009
    Posts
    1,689

    Re: Random numbers

    Put a srand(time()) on initialization.

  4. #4
    Join Date
    Sep 2012
    Posts
    10

    Re: Random numbers

    Quote Originally Posted by laserlight View Post
    You probably forgot to use srand to seed the PRNG.

    Also, if the numbers must be different on each iteration, then you would be better off creating an array of integers from 1 to 5 then shuffling the array with std::random_shuffle.
    Please explain me a bit more about it...i am really getting so confused..i am just a beginner in C++ Its only been few months that i am trying to learn it.

  5. #5
    Join Date
    Sep 2012
    Posts
    10

    Re: Random numbers

    Quote Originally Posted by ninja9578 View Post
    Put a srand(time()) on initialization.
    for (int j=1;j<=5;j++)
    {
    for (int i=1;i<=5;i++)
    {
    srand (i);
    printf ("First number: %d\n", rand() % 5);
    }
    printf ("\n");
    }
    Is this the way i should do? In this output also every time i am getting the same sets of random numbers...The output is always
    1 0 3 1 4
    1 0 3 1 4
    1 0 3 1 4
    1 0 3 1 4
    1 0 3 1 4

    What should i be doing to get outputs like..
    1 2 4 3 4
    3 2 3 1 3
    2 3 4 5 2
    etc...differents numbers between 1 and 5 very randomly..i am just a begginer please do suggest me some easy and efficient method of doing it.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,107

    Re: Random numbers

    Call srand once. Only once at the start of your program.

  7. #7
    Join Date
    Sep 2012
    Posts
    10

    Re: Random numbers

    Quote Originally Posted by GCDEF View Post
    Call srand once. Only once at the start of your program.
    Means i should call srand outside the for loop?? How do i avoid having the zeros in the random number?? I just need the number among 1 to 5.

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,294

    Re: Random numbers

    Quote Originally Posted by en061
    Means i should call srand outside the for loop?
    Before the loop, yes. One way is to call it as near as possible to the beginning of the global main function.

    Quote Originally Posted by en061
    How do i avoid having the zeros in the random number?? I just need the number among 1 to 5.
    With the current method that you are using, you can add 1 to the result to shift the range from [0, 4] to [1, 5].
    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

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center