Advertisement
Guest User

Untitled

a guest
Jul 10th, 2016
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.11 KB | None | 0 0
  1. /* autor: faramir */
  2.  
  3. #include <stdlib.h>
  4. #include <float.h>
  5. #include <math.h>
  6. #include <time.h>
  7.  
  8. int maxN = 1000;
  9. int maxIndex = -1;
  10.  
  11. unsigned int find_seed(int N, double x) {
  12.     for (unsigned int i = 1; i != 0; ++i) {
  13.         srand(i);
  14.         int _N=(rand() % maxN) + 1;
  15.         if (_N==N) {
  16.             double _x = (double)rand() / RAND_MAX;
  17.             if (fabs(_x - x) < 1e-14) return i;
  18.         }
  19.     }
  20.     return 0;
  21. }
  22.  
  23. int guess_max(double x, int N, int count) {
  24.     if (maxIndex < 0) {
  25.         unsigned int seed = find_seed(N, x);
  26.         srand(seed);
  27.         int dummyN = (rand() % maxN) + 1;
  28.         if (dummyN == N) {
  29.             double max = DBL_MIN;
  30.             for (int i = 1; i <= N; ++i) {
  31.                 double _x = (double)rand() / RAND_MAX;
  32.                 if (_x > max) {
  33.                     max = _x;
  34.                     maxIndex = i;
  35.                 }
  36.             }
  37.         } else {
  38.             /* ziarenko losowosci nie zostalo znalezione */
  39.             srand(time(NULL));
  40.             maxIndex = (rand() % N) + 1;
  41.         }
  42.     }
  43.     return maxIndex == count;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement