Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <Windows.h>
  3. #include <cstdlib>
  4. #include <string>
  5. #include <fstream>
  6. #include <time.h>
  7.  
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12. std::cout << "------------------------------Random Drug Screen (RDS) Written by Patrick Ward for LLCHC--------------------------------nn";
  13. std::cout << " This Program will choose a name randomly in the Name.txt filenn";
  14. std::cout << " Higher chances can be set per name.nn";
  15. std::cout << "------------------------------------------------------------------------------------------------------------------------nn";
  16.  
  17. ifstream nameBank;
  18. nameBank.open("Names.txt");
  19. string names[100]; //holds the names, currently 100
  20. string randomNames[2]; //Holds the names after their generated from names.txt
  21. int nameArray = 0;
  22. int randomNumber;
  23. while (nameBank.good()) {
  24. getline(nameBank, names[nameArray]);// reads the names from names.txt and puts the namearray in names[]
  25. nameArray++;
  26. }
  27. for (int iteration = 0; iteration < 2; iteration++) { //makes the program iterate 2 times, giving you 2 random names
  28. srand(time(NULL));//using time to generate a random number to pick a name with
  29. randomNumber = rand() % 100 + 1; //creates a random number between 1 and 100
  30. randomNames[iteration] = names[randomNumber];
  31. }
  32. for (int iteration = 0; iteration < 2; iteration++) {
  33. cout << randomNames[iteration] << endl; //outputs 2 names at once
  34. }
  35. nameBank.close();
  36. system("pause");
  37. }
  38.  
  39. #include <fstream>
  40. #include <iostream>
  41. #include <random>
  42. #include <string>
  43. #include <vector>
  44.  
  45. std::vector<std::string> read_names(const char* filename)
  46. {
  47. std::ifstream in{ filename };
  48. std::vector<std::string> names;
  49.  
  50. std::string name;
  51. while (getline(in, name))
  52. if (!name.empty())
  53. names.push_back(std::move(name));
  54.  
  55. return names;
  56. }
  57.  
  58. int main()
  59. {
  60. const auto first_names = read_names("Names.txt");
  61.  
  62. std::mt19937 rng{ std::random_device{}() };
  63. std::uniform_int_distribution<size_t> first_dist{ 0, first_names.size() - 1 };
  64. for (int i = 0; i < 100; ++i) {
  65. int first = first_dist(rng);
  66. std::cout << first_names[first] << 'n';
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement