This message was edited by bluj91 at 2006-11-15 15:30:40
: (As the tradition dictates) Check
: www.codepedia.com/1/CppFunction
: www.codepedia.com/1/CppArray
: www.codepedia.com/1/CppCout
: www.codepedia.com/1/CppRand
:
: See ya,
:
:
: bilderbikkel
:
:
To get the rand() to be really random you must set the seed using srand(). I usually set this to the return value of time(NULL).
ie.
#include <cstdio>
#include <cstdlib>
using namespace std;
int main(int argc, char ** argv)
{
srand(time(NULL));
rand();
...//what ever is next
}
You can also exact an arrange on rand() by using the modulus operator, %, which returns the remainder of an integeral division.
ie.
#include <cstdio>
#include <cstdlib>
using namespace std;
#define __RANDMIN__ 50
#define __RANDMAX__ 100
int main(int argc, char **argv)
{
srand(time(NULL));
rand() % __RANDMAX__ + __RANDMIN__; // to generate a random number between __RANDMIN__, 50, and __RANDMAX__ 100
//what ever goes next
}
The only tricky thingy with this if you want to generate really big random number or negative random numbers.
Last of all, the tricky thing I found when I first started out with arrays is that you access the element of an array using 0, not 1. And also to declare an array of five elements you use 5, not 4.
ie.
#include <iostream>
using namespace std;
int main(void)
{
int a[5] = {0,10,20,30,40};
cout << a[4]; //would the fifth element, 40, not the forth element, 30.
return 0;
}
COol