Hi. Here is the code
I don't understand a few things here: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; }
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.
http://i3physics.com/blog
*-*-*-*__ C++ revolutionized the modern programming language, but what happen to C+? Programming is just a study of chemistry __*-*-*-*
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".
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?
http://i3physics.com/blog
*-*-*-*__ C++ revolutionized the modern programming language, but what happen to C+? Programming is just a study of chemistry __*-*-*-*
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.
I'll go through your code line by line with you when I get home.![]()
The for-loop will allow all actions within to occur 20 times. I think you've worked that bit out.
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:cout << setw( 10 ) << ( 1 + rand() % 6 );
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:Code:if ( i % 5 == 0 ) cout << endl;
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.
It's this part of the code:
As said, a new line is started once the variable 'i' is exactly divisible by 5.Code:rand() % 6
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
http://i3physics.com/blog
*-*-*-*__ C++ revolutionized the modern programming language, but what happen to C+? Programming is just a study of chemistry __*-*-*-*
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.
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:
Is equivalent toCode:1 + rand() % 6
Both of the above are in infix notation.Code:(rand() % 6) + 1
This is going way back, but if we use prefix notation for the above calculation we have:
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'.Code:rand() 6 1 % +
Which do you prefer?
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?
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
http://i3physics.com/blog
*-*-*-*__ C++ revolutionized the modern programming language, but what happen to C+? Programming is just a study of chemistry __*-*-*-*
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks