what is the easiest way to get a set of random numbers and set them as a variable?
what is the easiest way to get a set of random numbers and set them as a variable?
im new
Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.
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.
Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.
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; }
Last edited by Stoned_Coder; 02-27-2002 at 08:59 AM.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
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.