Advertisement
dmilicev

generator_of_random_numbers_v1.c

Nov 7th, 2019
360
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.31 KB | None
  1. /*
  2.  
  3.     generator_of_random_numbers_v1.c
  4.  
  5.     Generate random integer numbers between lower and upper, including them.
  6.  
  7.  
  8.     https://www.geeksforgeeks.org/generating-random-number-range-c/
  9.  
  10.  
  11.     You can find all my C programs at Dragan Milicev's pastebin:
  12.  
  13.     https://pastebin.com/u/dmilicev
  14.  
  15.     https://www.facebook.com/dmilicev
  16.  
  17. */
  18.  
  19. #include <stdio.h>
  20. #include <time.h>           // for random numbers, for function rand()
  21. //#include <stlib.h>        // RAND_MAX is defined in stdlib.h
  22.  
  23.  
  24. // generate and return random integer number between lower and upper, including them.
  25. int get_random_integer_from_lower_to_uppper(int lower, int upper)
  26. {
  27.     // generate and return random number between 0 and upper-1
  28.     //return( rand() % upper );
  29.  
  30.     // generate and return random number between 1 and upper
  31.     //return( rand() % upper + 1 );
  32.  
  33.     // generate and return random integer number between lower and upper
  34.     return( (rand() % (upper - lower + 1)) + lower );
  35. }
  36.  
  37.  
  38. int main()
  39. {
  40.     int i;
  41.     time_t t;   // for random number generator, don't forget to #include <time.h>
  42.  
  43.     // Intializes random number generator, should only be called once!
  44.     srand((unsigned) time(&t));
  45.  
  46.     for(i=0;i<20;i++)       // print 20 random integers to test function
  47.         printf("\n random number: %2d ", get_random_integer_from_lower_to_uppper(1,10) );
  48.  
  49.  
  50.     return 0;
  51. }
Advertisement
RAW Paste Data Copied
Advertisement