473,406 Members | 2,371 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,406 software developers and data experts.

Random sign

I'm creating this program to test an elementary
school kids math skills. They have five options to
choose from

1 = '+'
2 = '-'
3 = '*'
4 = '/'
0 = To have the computer choose.
My Problem is that when you choose " 0 " the
randomization doesn't work consistently. The computer
does choose the mathematical sign but it looses its
random consistancy, for example; the uwer chooses 0 ( to
let the computer choose the mathematical sign ) and this
is what it would look like:
6 * 6 =
6 * 9 =
9 * 5 =
4 * 7 = etc......
Now, this is what it SHOULD look like:
9 - 4 =
5 * 7 =
4 + 5 =
6 + 8 = ....etc
Sorry if the code is a little messy, I didn't know
how to attach a message, so I copied it straight from the
file.

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
using namespace std;
int Random_num ( void );
int main()
{

unsigned int counter,
number1, number2,
panswer,
R_answer,
choice,
point;
double final_score;
enum { add = 1, subtract, multiply, division };

cout << " Welcome to the Elementary Math Help. This
program will test your n/"
<< " skills in Addition, Subtraction,
Multiplication, and Division."<< endl;
cout << " Choose 1 for addition \n"
<< " Choose 2 for subtraction \n"
<< " Choose 3 for multiplication \n"
<< " Choose 4 for Division \n "
<< " or press 0 for the computer to choose \n";
cin >> choice;
counter = 0;
point = 0;
srand( time (0) );
if ( choice == 0 )
choice = rand()% 3 + 1;
while ( counter != 5 ){
R_answer = 0;

panswer = 0;

number1 = Random_num();

number2 = Random_num();

switch ( choice ){

case add:

R_answer = number1 + number2;

cout << number1 << " + " << number2 << " = ";

counter++;

cin >> panswer;

break;

case subtract:

R_answer = number1 - number2;

cout << number1 << " - " << number2 << " = ";

counter++;

cin >> panswer;

break;

case multiply:

R_answer = number1 * number2;

cout << number1 << " * " << number2 << " = ";

counter++;

cin >> panswer;

break;

case division:

R_answer = number1 / number2;

cout << number1 << " / " << number2 << " = ";

counter++;

cin >> panswer;

break;

default: cout << " Program Bug. ";

break;

}

if ( panswer == R_answer )

point++;
}
final_score = static_cast <double> ( point ) / 2;
if ( final_score <= .75 )
cout << " You scored low, please try again. " << endl;

cout << "Your score is " << setprecision (2)
<< setiosflags ( ios :: fixed | ios :: showpoint )
<< final_score << endl;
return 0;
}
int Random_num ( void )
{
int num;
num = 1 + rand() % 9;

return num;
}

Nov 16 '05 #1
2 1251
"John" <tr****@hotmail.com> wrote in message
news:00****************************@phx.gbl...

My Problem is that when you choose " 0 " the
randomization doesn't work consistently. The computer
does choose the mathematical sign but it looses its
random consistancy, for example; the uwer chooses 0 ( to
let the computer choose the mathematical sign ) and this
is what it would look like: <snip> srand( time (0) );
if ( choice == 0 )
choice = rand()% 3 + 1;
You should only seed the random number generator (srand) once, not every time
you choose. But that's not the problem...
if ( choice == 0 )
choice = rand()% 3 + 1;
while ( counter != 5 ){


You're choosing the random sign outside your while loop. Therefore it only
gets randomly set once. You want to set it inside the loop so it changes each
time through the loop.

Ken
Nov 16 '05 #2
Thank you for your help but I have already tried to
put the if statement in the while loop. and it does the
same exact thing. So what do you think

P.S

I forgot to mention, and I think think it's quite
obvious, that this is a win32 project. Also, I'm not
allowed to use pointers or arrays yet or anything beyond
functions.
-----Original Message-----
"John" <tr****@hotmail.com> wrote in message
news:00****************************@phx.gbl...

My Problem is that when you choose " 0 " the
randomization doesn't work consistently. The computer
does choose the mathematical sign but it looses its
random consistancy, for example; the uwer chooses 0 ( to let the computer choose the mathematical sign ) and this
is what it would look like:<snip>
srand( time (0) );
if ( choice == 0 )
choice = rand()% 3 + 1;


You should only seed the random number generator (srand)

once, not every timeyou choose. But that's not the problem...
if ( choice == 0 )
choice = rand()% 3 + 1;
while ( counter != 5 ){
You're choosing the random sign outside your while loop.

Therefore it onlygets randomly set once. You want to set it inside the loop so it changes eachtime through the loop.

Ken
.

Nov 16 '05 #3

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

Similar topics

5
by: Alistair | last post by:
Hello folks... this is my first post in here. I'm new to ASP having done all my previous work in Flash and bog standard HTML. Only been learning for a couple of weeks. anyway...I have been...
11
by: Dr John Stockton | last post by:
Q1 : Given an array such as might have been generated by var A = is there a highly effective way of reducing it to - i.e. removing the undefineds and shifting the rest down? ...
4
by: Maziar Aflatoun | last post by:
Hi everyone, I have the following code in my class method TheSeed = (int)DateTime.Now.Ticks; Random rndNum = new Random(TheSeed); RandNum = rndNum.Next(0, TotalRows);...
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...
48
by: Jimmy | last post by:
thanks to everyone that helped, unfortunately the code samples people gave me don't work. here is what i have so far: <% Dim oConn, oRS, randNum Randomize() randNum = (CInt(1000 * Rnd) + 1) *...
16
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I generate a random integer from 1 to N?...
41
by: Zytan | last post by:
Anyone do any tests on it? I would assume it has improved since C's rand(), but who knows. For some reason, and it could just be coincidence, I seem to see patterns. But even a crappy rand()...
11
by: Masud | last post by:
hi, for a test i want to generate different random numbers between different ranges in a single loop. I tried to solve in the following way but it always profile same value. I am looking for...
34
by: Johannes Baagoe | last post by:
About Math.random(), ECMA 262 just says "Returns a number value with positive sign, greater than or equal to 0 but less than 1, chosen randomly or pseudo randomly with approximately uniform...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.