'll show you one way to do it...
#include <time.h> //required for srand
#include <conio.h> //required for getch()
#include <stdio.h> //required for getch()
#include <iostream.h> //required for cout
#include <stdlib.h> //required for rand
using namespace std;
int main(int argc, char** argv[]) {
srand(time(NULL)); //sorry I don't remember
int rand_val; //random value
int max_val = 10; //max value
rand_var = rand()%(max_var+1);//initi
cout << rand_var << endl; //prints out random value
getch(); //waiting...
}
#include <iostream.h>
#include <stdlib.h>
// this function will generate a random number within the range minvalue to maxvalue
int random(int minvalue,int maxvalue)
{
return (rand() % ((maxvalue + 1) - minvalue)) + minvalue;
};
int main(void)
{
int numbers,min,max,i=0;
srand((unsigned int)time(0)); // seed the random number one time, do not do this repeatedly (!)
cout << "How many random numbers to generate?";
cin >> numbers;
cout << "Enter minimum value>";
cin >> min;
cout << "Enter maximum value>";
cin >> max;
while(i++ < numbers)
{
cout << random(min, max) << endl;
};
return 0;
};
If you still want integers, you can multiply the double value with a power of 10 and obtain an integer with the first digits of the resulted fraction.
Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.
Regards,
Pratik.
...Bill
bill i wouldn't mind if u give me an idea on how to do both, cheers..