Click to See Complete Forum and Search --> : C++ question........random numbers


±Octavian
March 29th, 2001, 05:08 PM
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.

Deity
March 29th, 2001, 05:28 PM
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

mahdi
March 30th, 2001, 10:22 AM
I looked around some for you and found a good page the should help you with this here (http://www.bergen.org/AAST/Projects/CPP/lesson4/rand.html)
There should also be referances of this function in almost any C++ programming book.

±Octavian
March 30th, 2001, 01:29 PM
Originally posted by mahdi:
I looked around some for you and found a good page the should help you with this here (http://www.bergen.org/AAST/Projects/CPP/lesson4/rand.html)
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.

migel628
April 2nd, 2001, 03:13 PM
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.

DualP3Guy
April 13th, 2001, 09:34 PM
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&lt;time.h&gt;. 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&lt;time.h&gt;

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 aj11284@nc.rr.com. I am happy to answer all C++ questions that I can.