CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Feb 2003
    Posts
    53

    Please help with random number generation

    I am trying to generate two sets of 8 random numbers between (0 through 7) such that:
    1. none of the 8 number repeats
    2. the 8 numbers can repeat

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Please help with random number generation

    Originally posted by doumalc++
    I am trying to generate two sets of 8 random numbers between (0 through 7) such that:
    1. none of the 8 number repeats
    2. the 8 numbers can repeat
    For number 1, all you need is to initialize an array with 8 elements numbered 0,1,2,3,4,5,6,7. You then call the std::random_shuffle() function on the array. This shuffles the data around using a random number generator (you don't even have to supply one). The std::random_shuffle function is an STL algorithm that can be found in the <algorithm> header file.

    For the second, you initialize an array but for each element, the value will be the remainder value when you divide rand() by 8. (rand() is the random number generation function).

    I didn't supply any code, since this sounds like a school assignment.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Sep 2002
    Location
    Singapore
    Posts
    673
    You have to seed the generation with the current time using srand() when doing 1 & 2. Yes, even with random_shuffle(). Else random_shuffle() will keep giving you the same set of shuffled numbers.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by CBasicNet
    You have to seed the generation with the current time using srand() when doing 1 & 2. Yes, even with random_shuffle(). Else random_shuffle() will keep giving you the same set of shuffled numbers.
    Since this looks like a school assignment, I left that for the OP to discover.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Feb 2003
    Posts
    53
    Hey Paul, this is not a school assignment. I am learning c++ on my own.

    I did research on rand(), but found it difficult to understand.
    I successfully implemented random_shuffle() from looking up in the msdn online library.

    I still can't figure out how to generate the random numbers (0-7) such that they can repeat.

  6. #6
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    rand() returns a number between 0 and RAND_MAX (usually ~32000). To get a random number between 1 and 8, you would typically take the 8th modulus ( % operator ) or the random number. The other option is to divide by RAND_MAX and multiply by 8. This will give you a number between 0 and 7.

    int r = rand() % 8;
    int r = 8 * rand() / RAND_MAX;

  7. #7
    Join Date
    Feb 2003
    Posts
    53
    Thanks mwilliamson.
    I was having trouble with the syntax.

    btw. you meant:
    Code:
    rand() returns a number between 0 and RAND_MAX (usually ~32000).
    To get a random number between 0 and 7, you would typically take the 8th modulus ( % operator ) of the random number.
    The other option is to divide by RAND_MAX and multiply by 7. This will give you a number between 0 and 7.
    
    int r = rand() % 8;
    int r = 7 * rand() / RAND_MAX;
    Plus, shouldn't we set RAND_MAX as a multiple of 8 and 7 respectively? (so we can generate 0 and 7 respectively)
    Last edited by doumalc++; February 13th, 2003 at 12:35 AM.

  8. #8
    Join Date
    Sep 2002
    Location
    Singapore
    Posts
    673
    doumalc++ : if you are interested in the algorithm of shuffling, here's a simple program demonstrating that.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    int main()
    {
      int arr[10];
      //assign the numbers 1-10 to the elements of the array
      for(int cnt=0;cnt<10;++cnt)
        arr[cnt]=cnt+1;
    
      srand( (unsigned)time( NULL ));
      //The code below re-shuffles them 4 times
      for(int times=0;times<4;++times)
        for(int cnt2=0;cnt2<10;++cnt2)
        {
          int i = (rand()%10);
          int tmp = arr[i];
          arr[i] = arr[cnt2];
          arr[cnt2] = tmp;
        }
      
      //The code below displays them
      for (int cnt3=0;cnt3<10;++cnt3)
        cout<<arr[cnt3]<<endl;
    
      return 0;
    }

  9. #9
    Join Date
    Sep 2002
    Location
    Singapore
    Posts
    673

    [Edited : brain fart here]

    Originally posted by doumalc++

    Plus, shouldn't we set RAND_MAX as a multiple of 8 and 7 respectively? (so we can generate 0 and 7 respectively)
    The formula should be

    int r =8*rand()/(RAND_MAX+1);

    There is no way you can get 8 here.
    Last edited by CBasicNet; February 13th, 2003 at 02:31 AM.

  10. #10
    Join Date
    Feb 2003
    Posts
    53

    Re: [Edited : brain fart here]

    Originally posted by CBasicNet


    The formula should be

    int r =8*rand()/(RAND_MAX+1);

    There is no way you can get 8 here.

  11. #11
    Join Date
    Feb 2003
    Posts
    53

    Re: [Edited : brain fart here]

    Originally posted by CBasicNet


    The formula should be

    int r =8*rand()/(RAND_MAX+1);

    There is no way you can get 8 here.

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