Problems with random numbers

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

Moderator: Board moderators

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

Problems with random numbers

Post by lnr » Wed Oct 08, 2008 8:40 pm

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 » Thu Oct 09, 2008 12:03 am

"rand() / static_cast<double>(RAND_MAX)", or "drand48()".

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

Re: Problems with random numbers

Post by lnr » Thu Oct 09, 2008 4:17 am

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 » Thu Oct 09, 2008 11:07 am

Well, perhaps you didn't include the right header file (stdlib.h), or your compiler simply doesn't support it.

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

Re: Problems with random numbers

Post by lnr » Thu Oct 09, 2008 1:05 pm

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++”