You are using a version without Ads of this website. Please, consider donating:

### getting multiple outputs

hi , im very new to this , i got this program to give random number from 0 to 0.99 , but it only gives me one output at a time , i need 1000 outputs generated every time i run this , the code is as follows -

#include <iostream>
#include <ctime>
#include <fstream>
using namespace std;

//
// Generate a random number between 0 and 1
// return a uniform number in [0,1].
double unifRand()
{
return rand() / double(RAND_MAX);
}
//
// Generate a random number in a real interval.
// param a one end point of the interval
// param b the other end of the interval
// return a inform rand numberin [a,b].
double unifRand(double a, double b)
{
return (b-a)*unifRand() + a;
}
//
// Generate a random integer between 1 and a given value.
// param n the largest value
// return a uniform random value in [1,...,n]
long unifRand(long n)
{

if (n < 0) n = -n;
if (n==0) return 0;
/* There is a slight error in that this code can produce a return value of n+1
**
** return long(unifRand()*n) + 1;
*/
//Fixed code
long guard = (long) (unifRand() * n) +1;
return (guard > n)? n : guard;
}
//
// Reset the random number generator with the system clock.
void seed()
{
srand(time(0));
}

int main()
{
ofstream myfile;
myfile.open ("example.txt");
seed();
for (int i = 0; i < 20; ++i)
{
myfile << unifRand() << endl;
myfile.close();
}
return 0;
}
Topic archived. No new replies allowed.