|
-
March 29th, 2001, 05:08 PM
#1
Registered User
C++ question........random numbers
Before I start this a part of a homework assignment. I need to genrate random numbers, 100 of them. I am not sure if this a function that I must write or if C++ has something like this already. So main question is it a function or is it reseved command? If it is a function could some one help me with the algorithm of the fuction. If it is coded pleeas just point me to the right place. Note that if I recieve help From anyone on this there email address will be part of my comments due to professors requirements.
Q if this classifies as cheating please tell me or delete this post.
Faith is the substance of things not known and the Evidence of things not seen.
-
March 29th, 2001, 05:28 PM
#2
Registered User
C++ has a built in random function. the function is called "rand". Unfortunately I don't remember the exact syntax and have none of my reference materials nearby. This rand function may be part of a header file, possibly stdlib.h. Rand actually returns a floating point value between 0 and 1. In order to get random numbers in a specific range, such as 1 to 100, extra code is required. The rand function however, will generate the same psuedo random numbers each time the code is executed. There is another function called srand that creates a random number seed, changing the values that rand produces.
100 random numbers: //code may not be 100% correct, this is from memory
int iMyInt = 0;
int iHigh = 100 //highest possible random number
int iLow = 0 //lowest possible random number
for (int iLoop = iLow, iLoop < iHigh, ++iLoop) {
iMyInt = rand * (iHigh - iLow) + iLow //generates random number & stores in myint
cout << iMyInt << endl;
}
Again, I stress that this code is from memory, and may not be entirely accuate. The functions rand and srand are correct though.
Good Luck
A bored admin is a very dangerous person...
-
March 30th, 2001, 10:22 AM
#3
I looked around some for you and found a good page the should help you with this here
There should also be referances of this function in almost any C++ programming book.
-
March 30th, 2001, 01:29 PM
#4
Registered User
Originally posted by mahdi:
I looked around some for you and found a good page the should help you with this here
There should also be referances of this function in almost any C++ programming book.
Thanks bud from what I have seen from that website that is pehaps the best one I have seen. I will email you the code when I get it finished.
Thanks Deity as well. You both have been a great help.
-
April 2nd, 2001, 02:13 PM
#5
Registered User
I usually use the randomize(x) command. I believe that is actually C though. If I can find an example using that, then I'll post it.
-
April 13th, 2001, 08:34 PM
#6
Whoa, slow down. It is much easier than that. Here is what you have to do. First, include the time.h file. That is Microsoft so just do #include<time.h>. Then at the very top of your int main or void main put srand((unsigned)time(NULL)); that initializes randoms. Then anytime you want a random number, say I want int random to be a random number, I would do random = (rand() %10) +1; where the number after the modulus(%), in this case 10, is what you want your number to be max. This will make a number 1-10. Now an example:
// your headers here
#include<time.h>
int main()
{
srand((unsigned)time(NULL));
int random;
// your program here
//now you want a random say 1-6 you will do:
random = (rand() %6) +1;
//the rest of your program
return 0;
}//end main function
That is all!! If you want further explaination email me at [email protected]. I am happy to answer all C++ questions that I can.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|
Bookmarks