Advertisement
Foxyzboi

ГСПЧ с выбором диапазона

Nov 12th, 2020 (edited)
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <math.h>
  5.  
  6.  
  7. const float RAND_MAX_F = RAND_MAX;
  8.  
  9. // генерирует вещественное [0, 1]
  10. float get_rand() {
  11.     return rand() / RAND_MAX_F;
  12. }
  13.  
  14. // вещественное [min, max]
  15. float get_rand_range(const float min, const float max) {
  16.     return get_rand() * (max - min) + min;
  17. }
  18.  
  19. // целое [min, max]
  20. int get_rand_range_int(const int min, const int max) {
  21.     return rand() % (max - min + 1) + min;
  22. }
  23.  
  24.  
  25. int main() {
  26.     int i, r;
  27.  
  28.     srand(time(NULL));
  29.     for (i = 0; i < 20; i++) {
  30.         //r = ;
  31.         printf("%d\n", get_rand_range_int(-5, 5));
  32.     }
  33.    
  34.     return 0;
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement