-
January 21st, 2012, 02:51 PM
#1
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.
-
January 21st, 2012, 03:27 PM
#2
Re: Random Numbers
Hint: think multiplication, not addition
-
January 21st, 2012, 04:07 PM
#3
Re: Random Numbers
Originally Posted by Peter_B
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.
-
January 21st, 2012, 07:14 PM
#4
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.
-
January 21st, 2012, 07:36 PM
#5
Re: Random Numbers
Order of operation matters. It's the same in C++ as it is for all math.
-
January 22nd, 2012, 08:43 AM
#6
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.
-
January 22nd, 2012, 12:23 PM
#7
Re: Random Numbers
Originally Posted by Alterah
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.
Originally Posted by monarch_dodra
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.
-
January 23rd, 2012, 12:16 PM
#8
Re: Random Numbers
Originally Posted by iExtreme
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...
-
January 24th, 2012, 05:18 AM
#9
Re: Random Numbers
Originally Posted by VladimirF
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.
-
January 24th, 2012, 05:30 AM
#10
Re: Random Numbers
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.
-
January 25th, 2012, 09:55 AM
#11
Re: Random Numbers
Originally Posted by laserlight
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|