Guest User

Untitled

a guest
Jan 19th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <stddef.h>
  5. #include <string.h>
  6. #include <time.h>
  7.  
  8. #define _MIN_ -10
  9. #define _MAX_ 50
  10. #define _RANGE_ _MAX_-_MIN_
  11. int8_t min = _MIN_;
  12. int8_t max = _MAX_;
  13.  
  14. uint16_t rangeArray[_RANGE_];
  15.  
  16. static int8_t randInRangeInt8(int8_t min, int8_t max)
  17. {
  18. int8_t res = min + rand() / (RAND_MAX / (max - min + 1) + 1);
  19. return res;
  20. }
  21.  
  22.  
  23. int main()
  24. {
  25. printf("Random in range test!\n");
  26.  
  27. memset(rangeArray, 0, _RANGE_);
  28.  
  29. srand(time(NULL));
  30.  
  31. uint32_t t;
  32. for(t = 0; t < 5000; t++)
  33. {
  34. int8_t res = randInRangeInt8(min, max);
  35. if((res < min) || (res > max))
  36. {
  37. printf("ERROR!!!! res=%d, iterator t=%d\r\n", res, t);
  38. return -1;
  39. }
  40. else
  41. {
  42. if(rangeArray[res-_MIN_] < 65535)
  43. rangeArray[res-_MIN_]++;
  44. }
  45. }
  46.  
  47. printf("\r\n\r\n");
  48. for(t = 0; t < _RANGE_; t++)
  49. {
  50. printf("[%.3d]:%.3d => ", t+_MIN_, rangeArray[t]);
  51. int val = rangeArray[t];
  52. while(val > 0)
  53. {
  54. printf("*");
  55. val--;
  56. }
  57. printf("\r\n");
  58. }
  59.  
  60. return 0;
  61. }
Add Comment
Please, Sign In to add comment