Advertisement
Thaodan

find_random_var

Mar 4th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #if __cplusplus <= 199711L
  2. #   error You need a C++11 compilant compiler
  3. #endif
  4. #include <iostream>
  5. #include <random>
  6.  
  7. #define MAX_NUMBERS 1000
  8. #define RANGE_START 0
  9. #define RANGE_END 100
  10. using namespace std;
  11.  
  12. int main() {
  13.   // random var and input var
  14.   int random_vars[MAX_NUMBERS], input_var;
  15.   // define var to say we are succesfull or not and set it to false
  16.   bool succesfull=false;
  17.   // create random devices
  18.   random_device random_device0;
  19.   // init a discription of integer numbers starting with RANGE_START and ending with RANGE_END
  20.   uniform_int_distribution<int> distribtion(RANGE_START, RANGE_END);
  21.  
  22.   // now gen random numbers with our random device and our distribtion
  23.   for (int i=0; i < MAX_NUMBERS;i++)
  24.     random_vars[i]=distribtion(random_device0);
  25.  
  26.   // input dialog
  27.   cout << "Please Random var";
  28.   cin  >> input_var;
  29.  
  30.   // now check if input_var was found in our random numbers
  31.   for (int i=0; i < MAX_NUMBERS;i++)
  32.     {
  33.       // if we she found him do this:
  34.       if (random_vars[i] == input_var)
  35.     {
  36.       cout << "Number was element " << i+1 << " of random_vars" << endl; // say where the match was
  37.       succesfull=true; // say that we are succesfull
  38.       break; // break out of our loop since we are done here
  39.     }
  40.     }
  41.  
  42.   if ( succesfull == false ) // check if input_var was not found in random_vars
  43.     {
  44.       cout << "Number " << input_var << "not found" << endl; // respond to user
  45.       return 1; // return with uncessfull (negative logic)
  46.     }
  47.   else // if we succesfull just return to shell with succesfull (some logic here)
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement