473,396 Members | 1,927 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Having trouble adding random numbers

Hi. I am working on a math tutoring program which generates two random
numbers (from 1 to 500) and asks the user to add them. How can I check
to see whether or not they put in the correct total?? How do you add
random numbers that are generated anyway??

#include <iostream>
#include <cstdlib>
#include <time>
#include <conio>
using namespace std;

int main()
{
unsigned seed;

cout << "Enter a seed value: " ;
cin >seed;
srand(time(0));
cout << rand()%500 << endl;
cout << rand()%500 << endl;
cout << rand()%500 + rand()%500;
getch();
return 0;
}
Enter a seed value: 1
385
425
644

Apr 8 '07 #1
12 4433
Naya wrote:
Hi. I am working on a math tutoring program which generates two random
numbers (from 1 to 500) and asks the user to add them. How can I check
to see whether or not they put in the correct total?? How do you add
random numbers that are generated anyway??
You need to store the random numbers in a variable. Once they are
stored, they're no longer random.
Apr 8 '07 #2

"Naya" <na***********@hotmail.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
Hi. I am working on a math tutoring program which generates two random
numbers (from 1 to 500) and asks the user to add them. How can I check
to see whether or not they put in the correct total?? How do you add
random numbers that are generated anyway??
Same way you'd add any two numbers; it doesn't
matter where they came from.

int r(rand() + rand());

-Mike
Apr 8 '07 #3
"Naya" <na***********@hotmail.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
Hi. I am working on a math tutoring program which generates two random
numbers (from 1 to 500) and asks the user to add them. How can I check
to see whether or not they put in the correct total?? How do you add
random numbers that are generated anyway??

#include <iostream>
#include <cstdlib>
#include <time>
#include <conio>
using namespace std;

int main()
{
unsigned seed;

cout << "Enter a seed value: " ;
cin >seed;
srand(time(0));
cout << rand()%500 << endl;
int Rand1 = rand() % 500;
cout << Rand1 << endl;

You should be able to figure out the rest from that.
cout << rand()%500 << endl;
cout << rand()%500 + rand()%500;
getch();
return 0;
}
Enter a seed value: 1
385
425
644

Apr 8 '07 #4
On Apr 7, 10:53 pm, "Mike Wahler" <mkwah...@mkwahler.netwrote:
"Naya" <nadiamcint...@hotmail.comwrote in message

news:11**********************@q75g2000hsh.googlegr oups.com...
Hi. I am working on a math tutoring program which generates two random
numbers (from 1 to 500) and asks the user to add them. How can I check
to see whether or not they put in the correct total?? How do you add
random numbers that are generated anyway??

Same way you'd add any two numbers; it doesn't
matter where they came from.

int r(rand() + rand());

-Mike
When I do that, I get an error. Here's my program:
int main()
{
unsigned seed;
int r(rand()+ rand());

cout << "Enter a seed value: " ;
cin >seed;

srand(time(0));
cout << rand()%500 <<endl;
cout << rand()%500 <<endl;
r(rand() + rand());
getch();
return 0;
}

When I did that, I got two errors:
Call to nonfunction, and
R is assigned a value that is never used

Please excuse me if I seem dumb, but I am really new at C++; so don't
be too hard on me :-):-)

Apr 8 '07 #5
Naya wrote:
Hi. I am working on a math tutoring program which generates two random
numbers (from 1 to 500) and asks the user to add them. How can I check
to see whether or not they put in the correct total?? How do you add
random numbers that are generated anyway??
Your problem is that you have to generate only two random numbers, and
then calculate the sum. So you declare two variables

int rand1;
int rand2;

then you assign a rand() % 500 to each of them, and then you sum up
rand1 + rand2.

Regards,

Zeppe
Apr 8 '07 #6

"Naya" <na***********@hotmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
On Apr 7, 10:53 pm, "Mike Wahler" <mkwah...@mkwahler.netwrote:
>"Naya" <nadiamcint...@hotmail.comwrote in message

news:11**********************@q75g2000hsh.googleg roups.com...
Hi. I am working on a math tutoring program which generates two random
numbers (from 1 to 500) and asks the user to add them. How can I check
to see whether or not they put in the correct total?? How do you add
random numbers that are generated anyway??

Same way you'd add any two numbers; it doesn't
matter where they came from.

int r(rand() + rand());

-Mike

When I do that, I get an error. Here's my program:
Where are the headers?
int main()
{
unsigned seed;
int r(rand()+ rand());
Here, 'rand()' is called, but you haven't seeded the
generator yet. You'll get the same values every time
you run the program.

The above line calls 'rand()' twice, adds the two
results and initializes the type 'int' object with
that sum.
>
cout << "Enter a seed value: " ;
cin >seed;
Here, you request a seed value, but you never use it.
Not really an 'error', but pointless.
>
srand(time(0));
Here, you use time as the seed, which is a very
commone way to do it.
cout << rand()%500 <<endl;
cout << rand()%500 <<endl;
r(rand() + rand());
This statement is trying to call a function named 'r()',
but no such function has been declared.
getch();
Note that 'getch()' is not part of standard C++. Use
cin.get() or 'getchar()'.
return 0;
}

When I did that, I got two errors:
Call to nonfunction, and
Which is true. There's no function 'r'.
R is assigned a value that is never used
That's true as well. Again, not really an 'error',
just rather pointless to create a value and never use it.
This warning is referring to the 'r' in the second line
of 'main()'.
>
Please excuse me if I seem dumb, but I am really new at C++; so don't
be too hard on me :-):-)
I hope I wasn't. BTW which C++ book(s) are you using?

-Mike
Apr 8 '07 #7

Mike Wahler wrote:
"Naya" <na***********@hotmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
On Apr 7, 10:53 pm, "Mike Wahler" <mkwah...@mkwahler.netwrote:
"Naya" <nadiamcint...@hotmail.comwrote in message

news:11**********************@q75g2000hsh.googlegr oups.com...

Hi. I am working on a math tutoring program which generates two random
numbers (from 1 to 500) and asks the user to add them. How can I check
to see whether or not they put in the correct total?? How do you add
random numbers that are generated anyway??

Same way you'd add any two numbers; it doesn't
matter where they came from.

int r(rand() + rand());

-Mike
When I do that, I get an error. Here's my program:

Where are the headers?
int main()
{
unsigned seed;
int r(rand()+ rand());

Here, 'rand()' is called, but you haven't seeded the
generator yet. You'll get the same values every time
you run the program.

The above line calls 'rand()' twice, adds the two
results and initializes the type 'int' object with
that sum.

cout << "Enter a seed value: " ;
cin >seed;

Here, you request a seed value, but you never use it.
Not really an 'error', but pointless.

srand(time(0));

Here, you use time as the seed, which is a very
commone way to do it.
cout << rand()%500 <<endl;
cout << rand()%500 <<endl;
r(rand() + rand());

This statement is trying to call a function named 'r()',
but no such function has been declared.
getch();

Note that 'getch()' is not part of standard C++. Use
cin.get() or 'getchar()'.
return 0;
}

When I did that, I got two errors:
Call to nonfunction, and

Which is true. There's no function 'r'.
R is assigned a value that is never used

That's true as well. Again, not really an 'error',
just rather pointless to create a value and never use it.
This warning is referring to the 'r' in the second line
of 'main()'.

Please excuse me if I seem dumb, but I am really new at C++; so don't
be too hard on me :-):-)

I hope I wasn't. BTW which C++ book(s) are you using?

-Mike
Well, I am using Starting Out With C++ Early Objects 5th ed. by Tony
Gaddis, Judy Walters, and Godfry Muganda. The program that I am doing
is a math tutor program that requires me to create a program that
would:
(1) generate two random numbers
(2) ask the user to input their answer of the sum of the two random
numbers
(3) If the user's answer is correct, it dispays Congratulations! on
the screen.
(4) If the user's answer is not correct, it displays the correct
score.

I know that I may have the use the if else if statement in this
program. What do you think??

Apr 9 '07 #8
Naya wrote:
Mike Wahler wrote:
....
I know that I may have the use the if else if statement in this
program. What do you think??
I could write it without an if statement but that is irrelevant.

Here is a hint - find two random numbers:

int v1 = rand();
int v2 = rand();
add them

v1+v2

compare them

(v1+v2)==v3

Try to put the rest together yourself.
Apr 9 '07 #9

Gianni Mariani wrote:
Naya wrote:
Mike Wahler wrote:
...
I know that I may have the use the if else if statement in this
program. What do you think??

I could write it without an if statement but that is irrelevant.

Here is a hint - find two random numbers:

int v1 = rand();
int v2 = rand();
add them

v1+v2

compare them

(v1+v2)==v3

Try to put the rest together yourself.
I tried that, and this is what happened:
#include <iostream>
#include <cstdlib>
#include <time>
#include <conio>
using namespace std;

int main()
{

int v1 = rand()%500,
v2 = rand()%500,
v3;

cout << "\t\tMATH TUTOR 101\n\n";
cout << "\t\t ***********\n\n";
cout << "Here are your two numbers. " << endl;

srand(time(0));
cout << rand()%500 <<endl;
cout << rand()%500 <<endl;

v3 = v1 + v2;

cout << "The sum of these two values are" <<v3 <<endl;
getch();
return 0;
}

When I tried that, here was my output:
MATH TUTOR 101

***********

Here are your two numbers.
15
421
The sum of these two values are612

My sum stays the same no matter what numbers are put in. I tried, but
I couldn't get it to work.

Apr 9 '07 #10
Naya wrote:
[..]
I tried that, and this is what happened:
#include <iostream>
#include <cstdlib>
#include <time>
#include <conio>
using namespace std;

int main()
{

int v1 = rand()%500,
v2 = rand()%500,
So, here you initialise your 'v1' and 'v2' with some random
numbers. That's very good.
v3;

cout << "\t\tMATH TUTOR 101\n\n";
cout << "\t\t ***********\n\n";
cout << "Here are your two numbers. " << endl;

srand(time(0));
cout << rand()%500 <<endl;
cout << rand()%500 <<endl;
And here you output, what, two *other* random numbers? Don't
you want to actually output 'v1' and 'v2'?
>
v3 = v1 + v2;

cout << "The sum of these two values are" <<v3 <<endl;
getch();
return 0;
}

When I tried that, here was my output:
MATH TUTOR 101

***********

Here are your two numbers.
15
421
The sum of these two values are612

My sum stays the same no matter what numbers are put in. I tried, but
I couldn't get it to work.
Well, you might want to think about random numbers and what calling
'rand()' does...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 9 '07 #11

"Naya" <na***********@hotmail.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
>
Gianni Mariani wrote:
>Naya wrote:
Mike Wahler wrote:
...
I know that I may have the use the if else if statement in this
program. What do you think??

I could write it without an if statement but that is irrelevant.

Here is a hint - find two random numbers:

int v1 = rand();
int v2 = rand();
add them

v1+v2

compare them

(v1+v2)==v3

Try to put the rest together yourself.

I tried that, and this is what happened:
#include <iostream>
#include <cstdlib>
#include <time>
#include <conio>
using namespace std;

int main()
{
Call 'srand()' here.
>
int v1 = rand()%500,
v2 = rand()%500,
Now you have two random numbers.
v3;

cout << "\t\tMATH TUTOR 101\n\n";
cout << "\t\t ***********\n\n";
cout << "Here are your two numbers. " << endl;

srand(time(0));
cout << rand()%500 <<endl;
cout << rand()%500 <<endl;
... so why generate two more?
>
v3 = v1 + v2;

cout << "The sum of these two values are" <<v3 <<endl;
getch();
return 0;
}

When I tried that, here was my output:
MATH TUTOR 101

***********

Here are your two numbers.
15
421
The sum of these two values are612

My sum stays the same no matter what numbers are put in. I tried, but
I couldn't get it to work.
As I wrote in my other reply:

'rand()' is called, but you haven't seeded the
generator yet. You'll get the same values every time
you run the program.

Call 'srand()' *before* calling 'rand()' if you want
different numbers each time.

-Mike


Apr 9 '07 #12

Mike Wahler wrote:
"Naya" <na***********@hotmail.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...

Gianni Mariani wrote:
Naya wrote:
Mike Wahler wrote:
...
I know that I may have the use the if else if statement in this
program. What do you think??

I could write it without an if statement but that is irrelevant.

Here is a hint - find two random numbers:

int v1 = rand();
int v2 = rand();
add them

v1+v2

compare them

(v1+v2)==v3

Try to put the rest together yourself.
I tried that, and this is what happened:
#include <iostream>
#include <cstdlib>
#include <time>
#include <conio>
using namespace std;

int main()
{

Call 'srand()' here.

int v1 = rand()%500,
v2 = rand()%500,

Now you have two random numbers.
v3;

cout << "\t\tMATH TUTOR 101\n\n";
cout << "\t\t ***********\n\n";
cout << "Here are your two numbers. " << endl;

srand(time(0));
cout << rand()%500 <<endl;
cout << rand()%500 <<endl;

.. so why generate two more?

v3 = v1 + v2;

cout << "The sum of these two values are" <<v3 <<endl;
getch();
return 0;
}

When I tried that, here was my output:
MATH TUTOR 101

***********

Here are your two numbers.
15
421
The sum of these two values are612

My sum stays the same no matter what numbers are put in. I tried, but
I couldn't get it to work.

As I wrote in my other reply:

'rand()' is called, but you haven't seeded the
generator yet. You'll get the same values every time
you run the program.

Call 'srand()' *before* calling 'rand()' if you want
different numbers each time.

-Mike
This problem has been SOLVED.
Thanks.

Apr 10 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Marc Dansereau | last post by:
Hi all I am new to this forum and to the c programming language. If I understand, the random() function in C return numbers that follow a uniform distribution U(0,1). Can somebody know how to...
1
by: bob | last post by:
I am a newbie. How do I add an array to a viewstate, then retreive the individual elements? Right now, I made an array with random numbers and when I go to access an element from different...
104
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
17
by: Sri | last post by:
How do you add an n-bit number in C? Regards, Sri
9
suzee_q00
by: suzee_q00 | last post by:
I will admit that lots of times the obvious eludes me and this is most likely one of those times but if anyone has any ideas on what I can do to make this code work, I would greatly appreciate it....
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
25
by: Subra | last post by:
Hi, What is the best way to find the 1000 largest numbers from the file having hell lot of entries ? Can you please help me to find out the way ? Do I need to go for B+ trees ?? Please help,...
13
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000,...
24
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.