Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <Windows.h>
- #include <cstdlib>
- #include <string>
- #include <fstream>
- #include <time.h>
- using namespace std;
- int main()
- {
- std::cout << "------------------------------Random Drug Screen (RDS) Written by Patrick Ward for LLCHC--------------------------------nn";
- std::cout << " This Program will choose a name randomly in the Name.txt filenn";
- std::cout << " Higher chances can be set per name.nn";
- std::cout << "------------------------------------------------------------------------------------------------------------------------nn";
- ifstream nameBank;
- nameBank.open("Names.txt");
- string names[100]; //holds the names, currently 100
- string randomNames[2]; //Holds the names after their generated from names.txt
- int nameArray = 0;
- int randomNumber;
- while (nameBank.good()) {
- getline(nameBank, names[nameArray]);// reads the names from names.txt and puts the namearray in names[]
- nameArray++;
- }
- for (int iteration = 0; iteration < 2; iteration++) { //makes the program iterate 2 times, giving you 2 random names
- srand(time(NULL));//using time to generate a random number to pick a name with
- randomNumber = rand() % 100 + 1; //creates a random number between 1 and 100
- randomNames[iteration] = names[randomNumber];
- }
- for (int iteration = 0; iteration < 2; iteration++) {
- cout << randomNames[iteration] << endl; //outputs 2 names at once
- }
- nameBank.close();
- system("pause");
- }
- #include <fstream>
- #include <iostream>
- #include <random>
- #include <string>
- #include <vector>
- std::vector<std::string> read_names(const char* filename)
- {
- std::ifstream in{ filename };
- std::vector<std::string> names;
- std::string name;
- while (getline(in, name))
- if (!name.empty())
- names.push_back(std::move(name));
- return names;
- }
- int main()
- {
- const auto first_names = read_names("Names.txt");
- std::mt19937 rng{ std::random_device{}() };
- std::uniform_int_distribution<size_t> first_dist{ 0, first_names.size() - 1 };
- for (int i = 0; i < 100; ++i) {
- int first = first_dist(rng);
- std::cout << first_names[first] << 'n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement