Problems with random numbers

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
lnr
Experienced poster
Posts: 142
Joined: Sat Jun 30, 2007 2:52 pm
Location: Dhaka,Bangladesh

Problems with random numbers

Post by lnr »

Can someone please tell me how to generate random numbers between 0 to 1?
Last edited by lnr on Fri Oct 17, 2008 8:46 pm, edited 1 time in total.
mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: Problems with random numbers

Post by mf »

"rand() / static_cast<double>(RAND_MAX)", or "drand48()".
lnr
Experienced poster
Posts: 142
Joined: Sat Jun 30, 2007 2:52 pm
Location: Dhaka,Bangladesh

Re: Problems with random numbers

Post by lnr »

Thanks mf.
But drand48() gets compile time error.
Last edited by lnr on Fri Oct 17, 2008 8:47 pm, edited 1 time in total.
mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: Problems with random numbers

Post by mf »

Well, perhaps you didn't include the right header file (stdlib.h), or your compiler simply doesn't support it.
lnr
Experienced poster
Posts: 142
Joined: Sat Jun 30, 2007 2:52 pm
Location: Dhaka,Bangladesh

Re: Problems with random numbers

Post 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;
}
Post Reply

Return to “C++”