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

Thread: Random Numbers

  1. #1
    Join Date
    May 2010
    Posts
    54

    Random Numbers

    Hey guys,

    I have an exercise that asks me to print numbers at random from the following set (using only a single statement):

    2, 4, 6, 8, 10

    Here's my statement:

    Code:
    cout << (2 + rand() % 9) << " ";
    which prints numbers at random between 2 and 10, now I can use the modulus operator in an if...else statement to print only even numbers but the exercise specifically requires using only one statement, can that be done using the conditional operator? and if not then how?

    Thanks in advance.

  2. #2
    Join Date
    Jan 2009
    Posts
    596

    Re: Random Numbers

    Hint: think multiplication, not addition

  3. #3
    Join Date
    May 2010
    Posts
    54

    Re: Random Numbers

    Quote Originally Posted by Peter_B View Post
    Hint: think multiplication, not addition
    Right, so the correct code is:

    Code:
    cout << (2 * rand() % 10) << " ";
    which successfully generates even numbers from the set provided, but I need to understand why it works...

    for example, I used

    Code:
    int number =  2 * (rand() % 10) ;
    and then set a breakpoint at that line and tested for values, values like 14 (7 * 2), 18 (9 * 2) were produced, unlike the first line of code above, how does the placement of the parentheses differentiate the evaluation of the expression?

    Thanks for your help.

  4. #4
    Join Date
    Aug 2009
    Posts
    440

    Re: Random Numbers

    The parenthesis force the: rand() % 10 to get executed first. So, say in both cases rand() returns 77. In the first case:

    Code:
    (2 * rand() % 10) //Here rand is 77 so...
    (2 * 77 % 10) //This gets evaluated left to write
    (154 % 10)
    4
    However with this case:
    Code:
    2 * (rand() % 10) //Here rand is 77 so...
    2 * (77 % 10) //The parenthesis is evaluated first
    2 * (7)
    14
    As always 2 * X will generate an even number.

    Hope this helps.

  5. #5
    Join Date
    Aug 2008
    Posts
    902

    Re: Random Numbers

    Order of operation matters. It's the same in C++ as it is for all math.

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

    Re: Random Numbers

    You might be interested in this alternative method, if there is no algebraic relation between the numbers:

    Code:
    #include <iosteam>
    #include <cstdlib>
    #include <ctime>
    
    const int random_numbers[] = {1, 7, 42, 199};
    
    int main()
    {
      std::srand(std::time(0));
      std::cout << random_numbers[rand() % 4] << std::endl;
    }
    You create an array containing all your numbers, and then you choose a random index.
    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.

  7. #7
    Join Date
    May 2010
    Posts
    54

    Re: Random Numbers

    Quote Originally Posted by Alterah View Post
    The parenthesis force the: rand() % 10 to get executed first. So, say in both cases rand() returns 77. In the first case:

    Code:
    (2 * rand() % 10) //Here rand is 77 so...
    (2 * 77 % 10) //This gets evaluated left to write
    (154 % 10)
    4
    However with this case:
    Code:
    2 * (rand() % 10) //Here rand is 77 so...
    2 * (77 % 10) //The parenthesis is evaluated first
    2 * (7)
    14
    As always 2 * X will generate an even number.

    Hope this helps.
    Much thanks for the clarification.

    Quote Originally Posted by monarch_dodra View Post
    You might be interested in this alternative method, if there is no algebraic relation between the numbers:

    Code:
    #include <iosteam>
    #include <cstdlib>
    #include <ctime>
    
    const int random_numbers[] = {1, 7, 42, 199};
    
    int main()
    {
      std::srand(std::time(0));
      std::cout << random_numbers[rand() % 4] << std::endl;
    }
    You create an array containing all your numbers, and then you choose a random index.

    Thanks for the help, I'm sure I'll need to use such methods in coming lessons.

  8. #8
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Random Numbers

    Quote Originally Posted by iExtreme View Post
    Code:
    int number =  2 * (rand() % 10) ;
    Just wanted to point that the above statement produces even numbers from 0 to 18, NOT from 2 to 10.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  9. #9
    Join Date
    May 2010
    Posts
    54

    Re: Random Numbers

    Quote Originally Posted by VladimirF View Post
    Just wanted to point that the above statement produces even numbers from 0 to 18, NOT from 2 to 10.
    Yeah, I noticed, that's why I asked what difference the placement of the parentheses make in this statement, thanks for your help.

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

    Re: Random Numbers

    I would say (2 * rand() &#37; 10) is wrong too. For starters, if RAND_MAX is sufficiently high, you could have overflow that results in implementation defined behaviour. Even if this is not the case, if rand() returns a multiple of 5, then your result will be 0, which is not one of the numbers that are supposed to be printed. Furthermore, 10 will never be printed.

    To fix this, I would reach for your incorrect solution of 2 * (rand() % 10), and modify it slightly by changing the 10 and using an addition.
    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

  11. #11
    Join Date
    May 2010
    Posts
    54

    Re: Random Numbers

    Quote Originally Posted by laserlight View Post
    I would say (2 * rand() % 10) is wrong too. For starters, if RAND_MAX is sufficiently high, you could have overflow that results in implementation defined behaviour. Even if this is not the case, if rand() returns a multiple of 5, then your result will be 0, which is not one of the numbers that are supposed to be printed. Furthermore, 10 will never be printed.

    To fix this, I would reach for your incorrect solution of 2 * (rand() % 10), and modify it slightly by changing the 10 and using an addition.
    It seems I somehow rushed testing my code because I missed that it never generates a 10, and in the few times I've run it, it never printed a 0... my mistake for seeding it before testing I guess.

    So, the correct code would be like this:

    Code:
    2 * (1 + rand() % 5)
    Much thanks for taking the time to help.

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