vakho

Modelireba Sak 2 N2 [BETA 2]

Dec 20th, 2013
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #define SIZE 8
  6. int ArrayRand[SIZE] = {1, 7, 11, 12, 15, 37, 98, 101};
  7.  
  8. int simulatedDivision(int a, int b, int q)
  9. {
  10.     int zi = a;
  11.     int modul = (1 << b) - q;
  12.  
  13.     int k = zi >> b;
  14.  
  15.     zi = zi & (~(~0 << b));
  16.  
  17.     zi += k*q;
  18.     while (zi >= modul)
  19.         zi -= modul;
  20.  
  21.     return zi;
  22. }
  23.  
  24. #define a 101
  25. #define c 1
  26. #define m 128 // 2^7
  27. int linearCongruentialGenerator(int zi_1)
  28. {
  29.     int zi = zi_1;
  30.     return (a*zi + c) & (~(~0 << 7));
  31. }
  32.  
  33. int discrete(float num)
  34. {
  35.     if (num > 0 && num <= 0.22)     return 21;
  36.     if (num > 0.22 && num <= 0.39)  return 10;
  37.     if (num > 0.39 && num < 1)      return 18;
  38.     return -1; // Shecdoma?
  39. }
  40.  
  41. int main()
  42. {
  43.     system("COLOR F0");
  44.  
  45.     int i = 0;
  46.     while (i < SIZE)
  47.     {
  48.         printf("ArrayRand[%d] = \t\t%d\t", i, ArrayRand[i]);
  49.         ArrayRand[i] = linearCongruentialGenerator(ArrayRand[i]);
  50.         printf("ArrayRand[%d] = \t%f\n", i, ( ArrayRand[i]/(float)m ));
  51.         i++;
  52.     }
  53.  
  54.     printf("\n\n");
  55.     i = 0;
  56.     while (i < SIZE)
  57.     {
  58.         printf("Discrete[%d] = \t%d\n", i, discrete( ArrayRand[i]/(float)m ));
  59.         i++;
  60.     }
  61.  
  62.     system("PAUSE");
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment