Sunday, April 15, 2012

Program 19TH in C++

PROBLEM STATEMENT :

Give an algorithm to generate random numbers between 1 and 2.

SOLUTION :

#include <cstdio>
#include <iostream.h>
#include <cstdlib>
#include <ctime>

float range(float a, float b)
{

srand(time(0));
float x = (float)rand()/RAND_MAX;
x*=(b-a);
x+=a;
return x;

}
int main()
{


cout<<" "<<range(1.0,2.0);

}

No comments:

Post a Comment