| | |
C++ multiplication program
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2005
Posts: 1
Reputation:
Solved Threads: 0
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?
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!
'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!
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]
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]
May 'the Google' be with you!
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)
#pragma hdrstop #include <condefs.h> #include <iostream.h> #include <conio.h> //--------------------------------------------------------------------------- #pragma argsused int main(int argc, char **argv) { int dicerole1; int dicerole2; cout << "this program will role 2 dice for 2 people randomly getting the numbers." << endl << endl; dicerole1 = rand() % 6 + 1; dicerole2 = rand() % 6 + 1; cout << "my role is:" << endl << "(" << dicerole1 << ", " << dicerole2 << ")" << endl << endl; dicerole1 = rand() % 6 + 1; dicerole2 = rand() % 6 + 1; cout << "your role is:" << endl << "(" << dicerole1 << ", " << dicerole2 << ")" << endl << endl; getch(); return 0; }
![]() |
Similar Threads
- Really need help with the interactive Calculator program, or will be dead (C++)
- Help getting this applet to work! (Java)
- How to implement multi-dimensional arrays in Python?? (Python)
- need help w/for loop (C++)
Other Threads in the C++ Forum
- Previous Thread: GET! GOT! GOTCHA! I still don't Get it...
- Next Thread: C++ menu problem
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






