solve makes 10 random numbers and save it in a file

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
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
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> >&)'
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?
Try this:

1
2
3
4
5
6
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);
}

tank u . Done correctly when I used c ++ 17
and tank u dutch ! its working for ver11

Vertical treatment.
(Also dev c ++)
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
#include<iostream>
#include <fstream>

using namespace std;

 double range(double f ,double l)
 	{return ((double)rand()/RAND_MAX) * (l - f) + f;}
 	
 	
 std::ifstream::pos_type filesize(const char* filename)
{
    std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
    return in.tellg(); 
}

template <class S>
void save(const char * filename,S array)
{
    FILE * f = fopen(filename, "wb");
    int size= array.size()*sizeof(array[0]);
   auto ap = &array[0];
    fwrite(ap,size,1,f) ;
    fclose(f);
}


template <class L>
void load(const char * filename,L  &array)
{
	int s=filesize(filename)/sizeof(array[0]);
	array.resize(s);
	FILE * f = fopen(filename, "rb");
	auto ap = &array[0];
	fread(ap,array.size()*sizeof(array[0]),1,f); 
   fclose(f);
}
	
 	int main()
 	{
 		string s,result;
 		char * filename=(char *)"10floats.dat";
 		for(int i=1;i<11;i++) s+=to_string(range(-2.5,3.5))+"\n";
 		save(filename,s);
 		load(filename,result);
 		cout<<result<<endl;
 		system("pause");
 	}
 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
#include <random>
#include <ctime>
#include <algorithm>
using namespace std;

mt19937 gen( time( 0 ) );
   
int main()
{
   const int N = 10;
   const double mn = -2.5, mx = 3.5;
   uniform_real_distribution<double> dist( mn, mx );
   
   double vals[N];
   generate_n( vals, N, [&dist](){ return dist( gen );  } );
   
   ofstream out( "randomData.txt" );
   for ( double x : vals ) out << x << '\n';
   for ( double x : vals ) cout << x << ' ';
}
Topic archived. No new replies allowed.