943,931 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 12657
  • C++ RSS
Feb 4th, 2005
0

C++ multiplication program

Expand Post »
First, let me say I am NOT asking for someone to write this program for me. I have to see how functions and operations are used in programs to learn to use them in my own program. And I need help understanding how to use rand() in the program I have to write. I have to write a program that is basically flash cards on the computer. It will distribute random numbers and ask the user to multiply and enter the product. I have to use 2 for loops (one is nested ) to do this. Can anyone tell me a site where I can find a better explination of rand than my text book has?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tad73831 is offline Offline
1 posts
since Feb 2005
Feb 4th, 2005
0

Re: C++ multiplication program

You might try Google.

'rand' returns an int value in some range. On MSVC it returns a number between 0 and 0x7FFF (32767).

Each time you call it, it returns a different number in a sequence that is pseudo-random. However, unless you use 'srand' to SEED the random sequence, it will always return the same sequence of numbers.

This can actually be a benefit, because you can write and test your program with rand and get the exact same sequence each time. Once you are happy with your program, introduce 'srand' to seed the number.

As a test, try this:

for (int i = 0; i < 100; i++) printf( "%d\n", rand() );

srand() takes an arg that is the seed value, and possible seeds are things like time:

srand( time(NULL) );

Internally the number is generated this way under MSVC, so you can see it is both simple and repeatable:

return(((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff);

There are much more 'random' random number generators used in encryption, but this should do for now!
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Feb 5th, 2005
0

Re: C++ multiplication program

This is from the Borland site:

rand() uses a multiplicative congruential random number generator with period 2 to the 32nd power to return successive pseudorandom numbers in the range from 0 to RAND_MAX. The symbolic constant RAND_MAX is defined in stdlib.h

Typical example:
[php]
#include <stdlib.h>
#include <stdio.h>
#include <time.h> // time()

int main(void)
{
int i;

// randomize(); // Borland
srand(time(NULL)); // other compilers
printf("Ten random numbers from 0 to 99\n\n");
for(i=0; i<10; i++)
printf("%d\n", rand() % 100);
return 0;
}
[/php]
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Feb 6th, 2005
0

Re: C++ multiplication program

you can also control the min and max numbers the rand will use by using the format avariable = rand() % (highnumber) + (lownumber); (highnumber and lownumber don't have to be variables you may use integer values you actually want instead) here is a simple program that roles dice as an example.


C++ Syntax (Toggle Plain Text)
  1. #pragma hdrstop
  2. #include <condefs.h>
  3. #include <iostream.h>
  4. #include <conio.h>
  5.  
  6.  
  7. //---------------------------------------------------------------------------
  8. #pragma argsused
  9. int main(int argc, char **argv)
  10. {
  11. int dicerole1;
  12. int dicerole2;
  13. cout << "this program will role 2 dice for 2 people randomly getting the numbers." << endl << endl;
  14. dicerole1 = rand() % 6 + 1;
  15. dicerole2 = rand() % 6 + 1;
  16. cout << "my role is:" << endl << "(" << dicerole1 << ", " << dicerole2 << ")" << endl << endl;
  17. dicerole1 = rand() % 6 + 1;
  18. dicerole2 = rand() % 6 + 1;
  19. cout << "your role is:" << endl << "(" << dicerole1 << ", " << dicerole2 << ")" << endl << endl;
  20. getch();
  21. return 0;
  22. }
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
evilsilver is offline Offline
84 posts
since Feb 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: GET! GOT! GOTCHA! I still don't Get it...
Next Thread in C++ Forum Timeline: C++ menu problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC