Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C - C++ Learning > generate random...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 4 Topic 4128 of 4218
Post > Topic >>

generate random double between 0 and 1

by carvalho.miguel@[EMAIL PROTECTED] Apr 27, 2008 at 01:15 PM

hello there,

im trying to generate a double precision random number between 0 and
1, but i keep getting 0 only.

class CGUtil
{
public:
static void startRandomNumberGenerator()
{
srand( static_cast<unsigned int>(time( 0 )) );
}

static double randomDouble()
{
return static_cast<double>(rand() / RAND_MAX);
}
};

int main( int argc, char** argv )
{
CGUtil::startRandomNumberGenerator();
for( int i = 0; i < 100; i++ )
std::cout << CGUtil::randomDouble() << std::endl;
return 0;
}

this outputs 0 for the entire loop, ive googled a bit and thats the
technique ppl use to generate pseudo-randoms between 0 and 1. im
casting it to double so theres no truncation (i think).

thanks in advance.
 




 4 Posts in Topic:
generate random double between 0 and 1
carvalho.miguel@[EMAIL PR  2008-04-27 13:15:36 
Re: generate random double between 0 and 1
Krzysztof Mitko <kmitk  2008-04-27 20:37:09 
Re: generate random double between 0 and 1
carvalho.miguel@[EMAIL PR  2008-04-27 13:40:41 
Re: generate random double between 0 and 1
"Jim Langston"   2008-04-28 10:24:19 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 12:56:44 CDT 2008.