#include <iostream>
#include <random>
#include <chrono>
#include <fstream>
int main()
{
// create a random engine
std::default_random_engine URNG { };
// let's seed the sucker using a seed sequence
// seed using either std::random_device or the system clock if random_device isn't available
std::seed_seq::result_type seeds[] { std::random_device {}(),
std::seed_seq::result_type(std::chrono::system_clock::now().time_since_epoch().count()) };
// create the seed sequence
std::seed_seq sseq(std::begin(seeds), std::end(seeds));
// seed the random engine
URNG.seed(sseq);
// create a distribution 100 - 200 inclusive
std::uniform_int_distribution<unsigned> DIST(100, 200);
// how many random numbers do you want?
constunsigned num_rnd { 100 };
// open the file for writing
std::ofstream file("test.txt");
// skip count
constunsigned count { 10 };
for (size_t loop = 0; loop < num_rnd; loop++)
{
// get a random number
unsigned num { DIST(URNG) };
file << num << ' ';
if (((loop + 1) % count) == 0) { file << '\n'; }
}
file.close();
}
That was incredibly helpful! Thank you so much :) Also the fact that you said, "let's seed the sucker using a seed sequence" ...was enough to fully make my day haha. Now… BEAR with me (lol bears), what exactly does a -seed sequence- do? *hides behind curtain*
Okay, now I'm attempting to store the numbers from the file into an array and I'm pretty confused on my placement. Any advice or help is greatly appreciated.
BTW, I hope you realize if you were actually writing 100 3-digit random numbers to a file they would be all schmooshed together. Your file will appear to contain a single 300-digit number. Trying to read that into an int will not work as you expect.
You are still writing only one number to your file, the last one generated.
Thank you so much for that. It's appreciated :) I didn't even realize I had the writing outside of the loop. I've been studying pretty much non-stop so it starts to get a little fuzzy.
This is what I have now with the array added after some editing from help and advice. I think it's running correctly now.
I haven't learned vectors yet so I'll start reading on them now.
#include <iostream>
#include <fstream>
#include <cstdlib> // srand/rand
#include <ctime> // time
#include <vector>
int main()
{
constunsigned num_elems = 100;
std::srand((unsigned) std::time(0));
// Arise vector, I invoke thee! (empty)
std::vector<int> vec;
for (size_t index = 0; index < num_elems; index++)
{
// create a random number and cram it back into the vector
vec.push_back((std::rand() % 101) + 100);
}
// did we actually get 100 numbers? let's check
for (size_t loop { }; loop < num_elems; loop++)
{
std::cout << vec[loop] << ' ';
}
std::cout << "\n\n";
std::cout << "Let's see that again (using iterators):\n";
for (auto it = vec.begin(); it != vec.end(); it++)
{
std::cout << *it << ' ';
}
std::cout << "\n\n";
std::cout << "Let's try a range based for loop:\n";
for (auto& it : vec)
{
std::cout << it << ' ';
}
std::cout << "\n\n";
std::cout << "Let's write our stored data out to a file:\n\n";
std::fstream file("randomData.txt", std::fstream::out); // assume the file opened OK
for (auto& it : vec)
{
file << it << ' ';
}
// done writing to the file, let's close it
file.close();
// open the file for reading
file.open("randomData.txt", std::fstream::in);
// we can either clear the vector of its contained data
vec.clear();
// or create a new vector
std::vector<int> vec_in;
// let's use the cleared vector for reading the data
int temp;
while (file >> temp)
{
vec.push_back(temp);
}
std::cout << "The data file contains:\n";
for (auto& it : vec)
{
std::cout << it << ' ';
}
std::cout << '\n';
}
omg you're hilarious and you made vectors look super cool and much easier to follow & understand than the professors I've had. I wish you were my teacher. *sad face*
Learning should be fun, and so should teaching. When I create a code example for someone here I most times add far more comments than I normally would.
It is my "Lucy 'splainin'" method.
If the instructor is bored by the material, why should the students care? (Hint, they don't.)
Please notice I not only showed you how to use vectors, I also showed you several different ways to "walk through" the container.
I'm subtle that way. Sneak up behind ya, and then slam you upside your head with additional C++ features that make using what you did ask about easier.
Too often instructors are given a book/guideline of how and what to teach that is years outdated. Written by people who don't know how the language has evolved.
Current C++ is not C. Nor is it C++98.
C++20 is about to be finalized. Why ignore C++11/14/17 features that make writing code MUCH easier?
Writing programs as if the language were stone knives and bear skins is IMO stupid.
I am not a professional C++ coder. For me this is a hobby. I am learning what C++ can do as well.
There are people here who code professionally. When they offer up a code snippet they are really using well what C++ has to offer, though in different areas.
Two people who I've personally learned a lot from are JLBorges and Duthomas.
There are others, but those two have really challenged me to examine their code snippets and dissect them.