473,320 Members | 1,991 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,320 software developers and data experts.

subtraction and division using srand

2
Hi,

I'm a student taking a beginner's c++ course. I am trying to write a program that performes simple math operations on random numbers. I am using the
srand(int(time(0))); function to generate the random numbers. I have two variables that I'm using to perform the operations upon. I was wondering if there was a way to tell one of the variables to be less than the other so that negative answers are not generated during subtraction and division. Here is a snippet:

Expand|Select|Wrap|Line Numbers
  1. srand(int(time(0)));
  2.    vara = 1 + rand() % (1000 - 1 + 1);
  3.    varb = 1 + rand() % (1000 - 1 + 1);
  4.    answera = 0;
  5.    cout << "What is " << vara << '-' << varb << '?' << endl; 
  6.    cin >> response;
  7.    answera = vara - varb;
Don't know how far off I am....like I said just a beginner. Any advice would be helpful.
May 8 '07 #1
3 3060
mac11
256 100+
I was wondering if there was a way to tell one of the variables to be less than the other so that negative answers are not generated during subtraction and division.
I would just compare the numbers before doing the subtraction and swap them if the second one turns out to be larger. (New code in bold)
Expand|Select|Wrap|Line Numbers
  1. srand(int(time(0)));
  2.    vara = 1 + rand() % (1000 - 1 + 1);
  3.    varb = 1 + rand() % (1000 - 1 + 1);
  4.    answera = 0;
  5.  
  6.     if( varb > vara ) // if b is larger swap a and b
  7.     {
  8.         int tmp = vara;
  9.         vara = varb;
  10.         varb = tmp;    
  11.     }
  12.  
  13.    cout << "What is " << vara << '-' << varb << '?' << endl; 
  14.    cin >> response;
  15.    answera = vara - varb;
May 8 '07 #2
AdrianH
1,251 Expert 1GB
Hi,

I'm a student taking a beginner's c++ course. I am trying to write a program that performes simple math operations on random numbers. I am using the
srand(int(time(0))); function to generate the random numbers. I have two variables that I'm using to perform the operations upon. I was wondering if there was a way to tell one of the variables to be less than the other so that negative answers are not generated during subtraction and division. Here is a snippet:

Expand|Select|Wrap|Line Numbers
  1. srand(int(time(0)));
  2.    vara = 1 + rand() % (1000 - 1 + 1);
  3.    varb = 1 + rand() % (1000 - 1 + 1);
  4.    answera = 0;
  5.    cout << "What is " << vara << '-' << varb << '?' << endl; 
  6.    cin >> response;
  7.    answera = vara - varb;
Don't know how far off I am....like I said just a beginner. Any advice would be helpful.
The formula you gave: "vara = 1 + rand() % (1000 - 1 + 1);" You don't need the - 1 + 1 as they cancel each other out.

So what you have is vara and varb being assigned a value between 1 and 1000.

If you just want one to be always greater than the other, just check after you generate the numbers and swap them if they are reverse of what you want.


Adrian
May 8 '07 #3
sttip
2
The formula you gave: "vara = 1 + rand() % (1000 - 1 + 1);" You don't need the - 1 + 1 as they cancel each other out.

So what you have is vara and varb being assigned a value between 1 and 1000.

If you just want one to be always greater than the other, just check after you generate the numbers and swap them if they are reverse of what you want.


Adrian
Thank you for swap suggestion. I wrote the srand statement from reading directions in textbook. Maybe I misunderstood. Thanks for help.
May 8 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: August1 | last post by:
I've written an interface and implementation file along with a client source file that allows the use of an overloaded subtraction operator. However, when using the program, I'm running into a...
34
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1...
13
by: Jeremy Holdstadt | last post by:
This seems to be a C question to me. If it is not, I apologize. This command: awk 'BEGIN {srand();print srand()}' will give the number of seconds since the epoch, present time. Can any of you...
25
by: Merrill & Michele | last post by:
Many of us watched the World Series of Poker this last week and plotted how we were to take over that world. My current problem begins with shuffling the deck. For the apps I've written before,...
5
by: bruce.james.lee | last post by:
hi i have a problem with integer subtraction in C. printf("%d", c < (a - b)); a is got from a #define and is 0x80000000 and b is got from input and is also 0x80000000. c is ffffffff (-1)....
11
by: jtagpgmr | last post by:
I am currently using the gcc compiler on a cygwin platform, I am a beginner when it comes to programming in C and want to know why anytime I run the .exe with the following code I get a...
20
by: junky_fellow | last post by:
Hi, In my previous post I asked if sizeof may be implemented as a function. Somebody pointed a link that says that sizeof may calculated as follows with a warning that it is not guaranteed to...
5
by: Keflavich | last post by:
Hey, I have a bit of code that died on a domain error when doing an arcsin, and apparently it's because floating point subtraction is having problems. I know about the impossibility of storing...
12
by: Bill Cunningham | last post by:
I have read and studied and looked at references and can't find out how to do this. Here's where some real knowledge comes in now lets see who in clc really knows there stuff ;) main(void) {...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.