Advertisement
Guest User

Untitled

a guest
Jul 10th, 2016
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <stdlib.h>
  2. // gcc (GCC) 4.8.1
  3. // źródło: http://www.jbox.dk/sanos/source/lib/stdlib.c.html <- chciałem napisać funkcję, która by odgadywała seed'a, ale okazało się, że nie znam żandej operacji, która potrafi odwrócić przesunięcie bitowe w prawo.
  4.  
  5. static double * numbers = NULL;
  6. static int maxNumberId = 0;
  7.  
  8. unsigned int antymodulo(int a, int b) {
  9.     unsigned int antymodulo = 0;
  10.     while(antymodulo % a != b) {
  11.         antymodulo++;
  12.     }
  13.     return antymodulo;
  14. }
  15.  
  16. int generateSeedFromFirstElementOfArray(int element) {
  17.     unsigned int randResult = element, searchedSeed = 0;
  18.     int maxN = 1000;
  19.     randResult = antymodulo(maxN, randResult - 1); // ponieważ int maxN = 1000; oraz N = (rand() % maxN) + 1;
  20.     srand(searchedSeed);
  21.     while((rand()%maxN) != randResult) {
  22.         searchedSeed++;
  23.         srand(searchedSeed);
  24.     }
  25.     return searchedSeed;
  26. }
  27.  
  28. int guess_max(double x, int N, int count) {
  29.     if(numbers == NULL) {
  30.         numbers = new double[N];
  31.         long seed = generateSeedFromFirstElementOfArray(N);
  32.         srand(seed);
  33.         rand();
  34.         for(int i = 0; i < N; i++) {
  35.             numbers[i] = (double)rand() / RAND_MAX; // ponieważ printf("%.15f\n", (double)rand() / RAND_MAX );
  36.             if(numbers[maxNumberId] < numbers[i]) {
  37.                 maxNumberId = i;
  38.             }
  39.         }
  40.     }
  41.     if(count == maxNumberId+1) {
  42.         delete numbers;
  43.         numbers = NULL;
  44.         return 1;
  45.     }
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement