Advertisement
cd62131

NormalRandom

Mar 21st, 2014
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. double nrand(void) {
  5.   int i;
  6.   double r = 0;
  7.   for (i = 0; i < 12; i++) r += rand() / ((double) RAND_MAX + 1.0);
  8.   return r - 6;
  9. }
  10. int compare_double(const void *p1, const void *p2) {
  11.   return *(double *) p1 - *(double *) p2;
  12. }
  13. int main(void) {
  14.   int i;
  15.   const int n = 100;
  16.   double v[n];
  17.   srand((unsigned) time(NULL));
  18.   for (i = 0; i < n; i++) v[i] = nrand() * 280 + 750;
  19.   for (i = 0; i < n; i++) printf("%.1lf ", v[i]); puts("");
  20.   qsort(v, n, sizeof(double), compare_double);
  21.   for (i = 0; i < n; i++) printf("%.1lf ", v[i]); puts("");
  22.   return 0;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement