hi there!
can anyone tell me how to write a program to produce a random number?
please go easy on me im only a newbie at this c++ thing!
cheers in advance
Printable View
hi there!
can anyone tell me how to write a program to produce a random number?
please go easy on me im only a newbie at this c++ thing!
cheers in advance
Pretty simple.Quote:
Originally posted by boontune
hi there!
can anyone tell me how to write a program to produce a random number?
please go easy on me im only a newbie at this c++ thing!
cheers in advance
You seed the random number generator with the current time so you get different numbers each time it is run. The reason I modded the function rand() with 255 is to reduce the range of randomness.Code:#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main( void )
{
// Seed the random number generator
srand( time(NULL) );
cout << "Random number: " << rand() % 255 << endl;
return 0;
}
thanks alot mate
And new to the Boards, as well.
Top of your screen, right-hand side. Click on the "search" button and have a go.
This is one of those questions that's been discussed many, many times before.
Read the FAQ, as well. It will put you in good stead with those with itchy trigger fingers, if you know what I mean. ;)
(MrWizard's assistance aside, if I hadn't said something to you about this, somebody else would have, and I'm, typically, more gentle about these things than some others are.)
-Skipper