I'll help you create a C++ program that generates 20 million random floats between 1.2222 and 1.4444 and writes them to a file named db01.dat.
cpp
#include <iostream>
#include <fstream>
#include <random>
#include <chrono>
#include <iomanip>
int main() {
const int COUNT = 20000000; // 20 million floats
const float MIN_VALUE = 1.2222f;
const float MAX_VALUE = 1.4444f;
const std::string FILENAME = "db01.dat";
// Initialize random number generator with current time as seed
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<float> dist(MIN_VALUE, MAX_VALUE);
// Open file for binary writing
std::ofstream file(FILENAME, std::ios::binary);
if (!file.is_open()) {
std::cerr << "Error: Could not open file " << FILENAME << std::endl;
return 1;
}
// Start timing
auto start = std::chrono::high_resolution_clock::now();
std::cout << "Generating " << COUNT << " random floats..." << std::endl;
// Generate and write floats
for (int i = 0; i < COUNT; ++i) {
float value = dist(gen);
file.write(reinterpret_cast<const char*>(&value), sizeof(float));
// Progress indicator every 1 million numbers
if ((i + 1) % 1000000 == 0) {
std::cout << "Progress: " << (i + 1) / 1000000 << " million floats written" << std::endl;
}
}
// Close file
file.close();
// End timing
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::seconds>(end - start);
// Calculate file size
std::ifstream check_file(FILENAME, std::ios::binary | std::ios::ate);
std::streamsize file_size = check_file.tellg();
check_file.close();
std::cout << "\nCompleted!" << std::endl;
std::cout << "Time taken: " << duration.count() << " seconds" << std::endl;
std::cout << "File size: " << file_size / (1024.0 * 1024.0) << " MB" << std::endl;
std::cout << "File saved as: " << FILENAME << std::endl;
return 0;
}
Code Explanation
The program consists of several key components:
Headers and Constants:
<random>: Modern C++ random number generation facilities
<fstream>: File I/O operations
<chrono>: Time measurement
Constants define the count (20 million), value range (1.2222 to 1.4444), and filename
Random Number Generation:
std::random_device rd: Hardware-based random seed generator
std::mt19937 gen(rd()): Mersenne Twister pseudo-random generator (high quality and fast)
std::uniform_real_distribution<float> dist(MIN_VALUE, MAX_VALUE): Ensures uniform distribution of floats in the specified range
File Writing:
The file is opened in binary mode (std::ios::binary) for efficiency
Each float is written using file.write() with 4 bytes per float
Binary format is much faster and more space-efficient than text format
Progress Tracking:
Progress is displayed every 1 million floats to show the program is running
Timing is measured using std::chrono to report total execution time
File Details:
Each float takes 4 bytes in binary format
Total file size will be approximately 76.3 MB (20,000,000 × 4 bytes)
Binary format preserves exact floating-point precision
Alternative Text Format Version
If you prefer a human-readable text file instead of binary, here's a modified version:
cpp
#include <iostream>
#include <fstream>
#include <random>
#include <chrono>
#include <iomanip>
int main() {
const int COUNT = 20000000;
const float MIN_VALUE = 1.2222f;
const float MAX_VALUE = 1.4444f;
const std::string FILENAME = "db01.dat";
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<float> dist(MIN_VALUE, MAX_VALUE);
// Open file for text writing
std::ofstream file(FILENAME);
if (!file.is_open()) {
std::cerr << "Error: Could not open file " << FILENAME << std::endl;
return 1;
}
// Set precision for output
file << std::fixed << std::setprecision(6);
auto start = std::chrono::high_resolution_clock::now();
std::cout << "Generating " << COUNT << " random floats..." << std::endl;
for (int i = 0; i < COUNT; ++i) {
float value = dist(gen);
file << value << "\n";
if ((i + 1) % 1000000 == 0) {
std::cout << "Progress: " << (i + 1) / 1000000 << " million floats written" << std::endl;
}
}
file.close();
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::seconds>(end - start);
std::cout << "\nCompleted!" << std::endl;
std::cout << "Time taken: " << duration.count() << " seconds" << std::endl;
return 0;
}
The text format version writes one float per line with 6 decimal places precision. However, note that the text file will be significantly larger (approximately 180-200 MB) and take longer to write compared to the binary format.