what is the easiest way to get a set of random numbers and set them as a variable?
Printable View
what is the easiest way to get a set of random numbers and set them as a variable?
You can use the randamize function... Function rand(100).. So random numbers within 100 . TO store them into variables..
a=rand(100).. You have to include the file math.h
>>> Function rand(100)..
You may be able to on some compilers, but it is not standard.
use an array and a loop.....
get 10 random numbers between 1 and 40 ...
Code:#include <iostream>
#include<cstdlib>
#include <ctime>
using namespace std;
int main ()
{
srand((unsigned)time(NULL)); // seed random number generator with system time
int MyRand[10]={0}; // an array to store our random values;
for(int i=0;i<10;++i)
{
MyRand[i]=rand()%40+1; // get random number between 1 and 40 and store in array
cout<<MyRand[i]<<endl;
}
return 0;
}
This is a very simple task my friend....
Somewhere in your program perferibly at the beginning have
srand(clock());
to set the random seed.
and to assign a variable to be a random number from whatever
say... from 1 to 100.
num = rand()%99+1;
thats it
Thank you have a nice day.