Dec 29, 2020 at 10:27am UTC
Hi.
I have to write a program that makes 10 random numbers between -2.5 and 3.5 and save it in a file.
I have gone so far with the help of this forum, but can you take a look and solve the problem of this program?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstdlib>
using namespace std;
double GenerateRandom(double min, double max)
{
static bool first = true ;
if (first)
{
first = false ;
}
if (min > max)
{
std::swap(min, max);
}
return min + (double )rand() * (max - min) / (double )RAND_MAX;
}
int main()
{
ofstream file;
file.open("randomData.txt" );
for (int i = 0; i < 10; ++i)
{
random = GenerateRandom(-2.5, 3.5);
}
file << random; //printing into text file
file.close();
int array[11] = {};
ifstream is("random" );
int num = 0;
int x;
while (num < array[11] && is >> x)
array[num++] = x;
cout << "The random numbers are:" << "\n" ;
for (int i = 0; i < num; i++) {
cout << GenerateRandom(-2.5, 3.5) << endl;
}
is.close();
return 0;
}
Last edited on Dec 29, 2020 at 12:12pm UTC
Dec 29, 2020 at 10:53am UTC
makes 10 random numbers between -2.5 and 3.5 and save it in a file
Consider:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include <iostream>
#include <fstream>
#include <random>
using namespace std;
double GenerateRandom(double min, double max)
{
static auto Rand {std::mt19937 {std::random_device {}()}};
auto randNo {std::uniform_real_distribution<double > {min, max}};
return randNo(Rand);
}
int main()
{
const size_t maxno {10};
ofstream file("randomData.txt" );
if (!file)
return (cout << "Cannot open file for output\n" ), 1;
for (int i = 0; i < maxno; ++i)
file << GenerateRandom(-2.5, 3.5) << ' ' ;
file.close();
double array[maxno] {};
ifstream is("randomData.txt" );
if (!is)
return (cout << "Cannot open file for input\n" ), 2;
for (size_t num = 0; num < maxno && is >> array[num]; ++num);
cout << "The random numbers are:" << "\n" ;
for (int i = 0; i < maxno; ++i)
cout << array[i] << ' ' ;
cout << '\n' ;
is.close();
}
An example:
The random numbers are:
0.673347 -1.76038 2.28866 1.83882 -0.0567361 -1.4891 3.47898 -0.436967 -0.517713 -0.103128
Last edited on Dec 29, 2020 at 10:55am UTC
Dec 29, 2020 at 12:07pm UTC
Thanks for your reply. But when I compile this with dev c ++, I get the following error:
12 20 C:\Users\user\Documents\Untitled1.cpp [Error] no match for call to '(std::initializer_list<std::uniform_real_distribution<double> >) (std::initializer_list<std::mersenne_twister_engine<unsigned int, 32ull, 624ull, 397ull, 31ull, 2567483615u, 11ull, 4294967295u, 7ull, 2636928640u, 15ull, 4022730752u, 18ull, 1812433253u> >&)'
Dec 29, 2020 at 12:27pm UTC
This compiles OK with MS VS2019 as C++17. I don't know/use dev C++. Is there an option to compile as C++17?
Dec 29, 2020 at 1:15pm UTC
tank u . Done correctly when I used c ++ 17
Dec 29, 2020 at 1:36pm UTC
and tank u dutch ! its working for ver11