Page 1 of 1

Problems with random numbers

Posted: Wed Oct 08, 2008 8:40 pm
by lnr
Can someone please tell me how to generate random numbers between 0 to 1?

Re: Problems with random numbers

Posted: Thu Oct 09, 2008 12:03 am
by mf
"rand() / static_cast<double>(RAND_MAX)", or "drand48()".

Re: Problems with random numbers

Posted: Thu Oct 09, 2008 4:17 am
by lnr
Thanks mf.
But drand48() gets compile time error.

Re: Problems with random numbers

Posted: Thu Oct 09, 2008 11:07 am
by mf
Well, perhaps you didn't include the right header file (stdlib.h), or your compiler simply doesn't support it.

Re: Problems with random numbers

Posted: Thu Oct 09, 2008 1:05 pm
by lnr
Without "cstdlib" rand()/static_cast<double>(RAND_MAX) works.
In spite of using "cstdlib" Microsoft Visual C++ 2008 does not support drand48().

Code: Select all

#include<iostream>
#include<cstdlib>
using namespace std;

int main()
{
	//cout<<rand()/static_cast<double>(RAND_MAX)<<endl;
	cout<<drand48()<<endl;
	return 0;
}