Advertisement
cd62131

write random numbers

Jul 15th, 2019
704
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. static inline double r(double beg, double end) {
  5.   return (rand() + 1.) / (RAND_MAX + 2.) * (end - beg) + beg;
  6. }
  7. static inline void write_random(FILE *out, unsigned int upper) {
  8.   int n = rand() % upper;
  9.   for (int i = 0; i < n; ++i) { fprintf(out, "%f\n", r(-10., 10.)); }
  10. }
  11. int main(void) {
  12.   srand(time(NULL));
  13.   FILE *out = fopen("random.txt", "w");
  14.   write_random(out, 100);
  15.   fclose(out);
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement