+ Reply to Thread
Page 1 of 2
1 2 LastLast
Results 1 to 10 of 11

Thread: rand function

  1. #1
    Speaks fluent binary jwxie518 is on a distinguished road jwxie518's Avatar
    Join Date
    Jan 2009
    Location
    New York City
    Age
    18
    Posts
    1,089
    Blog Entries
    1

    rand function

    Hi. Here is the code
    Code:
    #include <iostream>
    #include <iomanip>
    #include <stdlib>
    
    int main()
    {
       for ( int i = 1; i <= 20; i++ )
       {
          cout << setw( 10 ) << ( 1 + rand() % 6 );
    
          if ( i % 5 == 0 )
               cout << endl;
       }
    return 0;
    }
    I don't understand a few things here:
    1. for the rand() function, the book said rand() % 6 would produce integers in the range 0 to 5. Then why don't we just do rand() % 7? Is it because we want to ignore the zero therefore we do 1 + rand() % 6?

    2. the % as the book explained is "we use the modulus operator in conjunction with rand". I don't get it.

    3. for i % 5 == 0, I am having trouble understanding it. I know if you want to know whether a number is odd or even you could do i % 2 == 0. But I don't get the use of it here. It seems like it's being use to "go to the next line" because in the book it shows:

    5 5 3 5 5
    2 4 2 5 5
    5 3 2 2 1
    5 1 4 6 4

    Thank you.
    John Wong's blog
    *-*-*-*__ C++ revolutionized the modern programming language, but what happen to C+? Programming is just a study of chemistry __*-*-*-*

  2. #2
    Programming Professional PythonPower is on a distinguished road PythonPower's Avatar
    Join Date
    Feb 2009
    Location
    England
    Posts
    228

    Re: rand function

    1. rand()%7 gives you a random number in the range [0...6]; rand()%6+1 gives a random number in the range [1...6]. Use whatever you need.

    2. This is one of those "tricks" that is nice to know. At least ideally, rand() returns every number uniformly. So there is roughly equal distribution of those with remainder r, mod m. Hence taking rand() mod m, will also give a random r in the range [0...m-1].

    3. i%5 == 0 checks to see if what the remainder when i is divided by 5 is. For example is i = 17 then i/5 = 3 and i%5 = 2 (since 3*5+2 = 17). 2 == 0 is not true so it evaluates to false. However, if i was 15 then i%5 would be 0 and i%5 == 0 would evaluate to true.

    i%5==0 returns true iff i is divisible by 5. This makes sense when you consider it is saying "if 5 divides i with no remainder".
    Old: 19543634061503069431898480521767684453431945494947 93961697675137336753 = 432363203127002885506543172618401 × 4520189026299330782353421687996961553
    New: How many ways can one partition 369?

  3. #3
    Speaks fluent binary jwxie518 is on a distinguished road jwxie518's Avatar
    Join Date
    Jan 2009
    Location
    New York City
    Age
    18
    Posts
    1,089
    Blog Entries
    1

    Re: rand function

    Hi Python, thank you for the help. But I am still with the followings:

    1. I fully understood #1 thank you.

    2. So how is rand() % 6 a conjunction with rand? I look at % as finding the remainder.... I see rand() will generate the same sequence every time we run and thus we need to use srand to seed a different sequence (random).

    3. I see. But what is the purpose of the i % 5 which leads to cout << endl; ???
    As the sequence generate:
    6 6 5 5 6
    5 1 1 5 3
    6 6 2 4 2
    6 2 3 4 1

    If this is the case, what is the purpose of i % 5 ?
    Is it because when i = 5, i (10) % 5 == 0 is true, then it goes to the next line, am I correct?
    John Wong's blog
    *-*-*-*__ C++ revolutionized the modern programming language, but what happen to C+? Programming is just a study of chemistry __*-*-*-*

  4. #4
    Programming Professional PythonPower is on a distinguished road PythonPower's Avatar
    Join Date
    Feb 2009
    Location
    England
    Posts
    228

    Re: rand function

    2. I'm not sure if I follow the question... You're correct about needing the use of srand(time(0)) or something similar to obtain different results each time.

    3. It could well be used to add a new line character every 5 blocks. Without seeing the code I can't be sure; but I expect you're right.
    Old: 19543634061503069431898480521767684453431945494947 93961697675137336753 = 432363203127002885506543172618401 × 4520189026299330782353421687996961553
    New: How many ways can one partition 369?

  5. #5
    Programmer Mathematix is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    104

    Re: rand function

    I'll go through your code line by line with you when I get home.

  6. #6
    Programmer Mathematix is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    104

    Re: rand function

    Quote Originally Posted by jwxie518 View Post
    Hi. Here is the code
    Code:
    #include <iostream>
    #include <iomanip>
    #include <stdlib>
    
    int main()
    {
       for ( int i = 1; i <= 20; i++ )
       {
          cout << setw( 10 ) << ( 1 + rand() % 6 );
    
          if ( i % 5 == 0 )
               cout << endl;
       }
    return 0;
    }
    The for-loop will allow all actions within to occur 20 times. I think you've worked that bit out.

    Code:
    cout << setw( 10 ) << ( 1 + rand() % 6 );
    This code allows a line to contain a width of 10 characters and prints the pseudo-random value determined by 1 + rand() % 6 with possible values {1, 2, 3, 4, 5, 6}.

    Code:
          
    if ( i % 5 == 0 )
          cout << endl;
    This will start a new line of text when the variable 'i' incremented by the loop is divisible by 5 exactly. Now to your questions:


    Quote Originally Posted by jwxie518 View Post
    I don't understand a few things here:
    1. for the rand() function, the book said rand() % 6 would produce integers in the range 0 to 5. Then why don't we just do rand() % 7? Is it because we want to ignore the zero therefore we do 1 + rand() % 6?
    Because by adding one to are preventing a value of zero being produced and a maximum value of 6. rand() % 7 will allow the values {0,1, 2, 3, 4, 5, 6}, so we guarantee not producing a value of zero for your code as presented.

    Quote Originally Posted by jwxie518 View Post
    2. the % as the book explained is "we use the modulus operator in conjunction with rand". I don't get it.
    It's this part of the code:

    Code:
    rand() % 6
    Quote Originally Posted by jwxie518 View Post
    3. for i % 5 == 0, I am having trouble understanding it. I know if you want to know whether a number is odd or even you could do i % 2 == 0. But I don't get the use of it here. It seems like it's being use to "go to the next line" because in the book it shows:

    5 5 3 5 5
    2 4 2 5 5
    5 3 2 2 1
    5 1 4 6 4

    Thank you.
    As said, a new line is started once the variable 'i' is exactly divisible by 5.

  7. #7
    Speaks fluent binary jwxie518 is on a distinguished road jwxie518's Avatar
    Join Date
    Jan 2009
    Location
    New York City
    Age
    18
    Posts
    1,089
    Blog Entries
    1

    Re: rand function

    Hi math, thanks for the help.
    I am so into this question is because how do i know that it's part of the code? I mean let say I am working on a project,
    rand() is a function set in the standard library, but how do i know if % 6 is the right way to write my code?
    what if i need to write rand() / 6 instead of rand() % 6?

    Just a little bit into the % 6
    John Wong's blog
    *-*-*-*__ C++ revolutionized the modern programming language, but what happen to C+? Programming is just a study of chemistry __*-*-*-*

  8. #8
    Programmer Mathematix is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    104

    Re: rand function

    Quote Originally Posted by jwxie518 View Post
    Hi math, thanks for the help.
    I am so into this question is because how do i know that it's part of the code?
    No problem.

    It isn't really important that you know that it is part of the code per se, just that when the project is built, that part of the code becomes part of the logic of the overall program because it isn't commented out.


    Quote Originally Posted by jwxie518 View Post
    I mean let say I am working on a project,
    rand() is a function set in the standard library, but how do i know if % 6 is the right way to write my code?
    Because part of C/C++ actual standard is to use 'infix notation'. This is actually part of programming language theory taught as part of computer science courses (or at least should be! I graduated 10 years ago this month!)

    Now, assuming that '%' has a higher precedence than '+' as it is for the calculation shown, we know that:

    Code:
    1 + rand() % 6
    Is equivalent to

    Code:
    (rand() % 6) + 1
    Both of the above are in infix notation.

    This is going way back, but if we use prefix notation for the above calculation we have:

    Code:
    + % rand() 6 1
    and for postfix notation...

    Code:
    rand() 6 1 % +
    "prefix notation" is also known as 'Polish Notation', and "postfix notation" is also known as 'Reverse Polish Notation'.

    Which do you prefer?

    Quote Originally Posted by jwxie518 View Post
    what if i need to write rand() / 6 instead of rand() % 6?

    Just a little bit into the % 6
    This is actually a very good question. Remember that the modulus returns the remainder of an integer divison, so it make no sense to use floats, or doubles, for example when using the modulus '%'.

    rand() / 6 won't return the remainder of the division, it will simply tell you how many times 6 goes into the value that rand returns, which is entirely different from the above. Secondly, when high-precision is required, dividing two integers can result in a loss of data since the fractional part of the division is thrown away.

    Does that make things any clearer?

  9. #9
    Speaks fluent binary jwxie518 is on a distinguished road jwxie518's Avatar
    Join Date
    Jan 2009
    Location
    New York City
    Age
    18
    Posts
    1,089
    Blog Entries
    1

    Re: rand function

    OMG. You save my life. LOL Thank you so muuuuuuch

    Do you mind that I kiss you? jkjk
    let me try to +rep you for that
    John Wong's blog
    *-*-*-*__ C++ revolutionized the modern programming language, but what happen to C+? Programming is just a study of chemistry __*-*-*-*

  10. #10
    Programmer Mathematix is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    104

    Re: rand function

    Quote Originally Posted by jwxie518 View Post
    OMG. You save my life. LOL Thank you so muuuuuuch

    Do you mind that I kiss you? jkjk
    let me try to +rep you for that
    Don't mention it, mate. Please save the kisses for someone else, though!

+ Reply to Thread
Page 1 of 2
1 2 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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