home *** CD-ROM | disk | FTP | other *** search
/ ftp.sunet.sepub/pictures / 2014.11.ftp.sunet.se-pictures.tar / ftp.sunet.se / pub / pictures / ACiD-artpacks / programs / unix / editors / gimp-plugins-unstable-0_99_23_tar.gz / gimp-plugins-unstable-0_99_23_tar / gimp-plugins-unstable-0.99.23 / gag / utils / rand-gen.c
C/C++ Source or Header  |  1998-02-19  |  1KB  |  62 lines

  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. #define DBL double
  7.  
  8. #define TABLE_SIZE    256
  9.  
  10. typedef struct {
  11.   DBL x, y;
  12. } Vector2d;
  13.  
  14. static int perm_tab[TABLE_SIZE];
  15. static Vector2d grad_tab[TABLE_SIZE];
  16.  
  17.  
  18. void main(void)
  19. {
  20.   int i, j, k, t;
  21.   DBL m;
  22.  
  23.   srand( time(NULL) );
  24.  
  25.   for (i = 0; i < TABLE_SIZE; i++)
  26.     perm_tab[i] = i;
  27.   for (i = 0; i < TABLE_SIZE; i++) {
  28.     j = rand () % TABLE_SIZE;
  29.     k = rand () % TABLE_SIZE;
  30.     t = perm_tab[j];
  31.     perm_tab[j] = perm_tab[k];
  32.     perm_tab[k] = t;
  33.   }
  34.   printf("static int perm_tab[TABLE_SIZE]= {\n  ");
  35.   for (i=0; i < TABLE_SIZE; i++)
  36.     {
  37.       if (i != 0) printf(",");
  38.       if ((i!= 0) && ((i & 15) == 0)) printf("\n  ");
  39.       printf(" %3d", perm_tab[i]);
  40.     }
  41.   printf("\n};\n\n");
  42.  
  43.   /*  Initialize the gradient table  */
  44.  
  45.   printf("static Vector2d grad_tab[TABLE_SIZE]= {\n  ");
  46.   for (i = 0; i < TABLE_SIZE; i++) {
  47.     do {
  48.       grad_tab[i].x = (double)(rand () - (RAND_MAX >> 1)) / (RAND_MAX >> 1);
  49.       grad_tab[i].y = (double)(rand () - (RAND_MAX >> 1)) / (RAND_MAX >> 1);
  50.       m = grad_tab[i].x * grad_tab[i].x + grad_tab[i].y * grad_tab[i].y;
  51.     } while (m == 0.0 || m > 1.0);
  52.     m = 1.0 / sqrt(m);
  53.     grad_tab[i].x *= m;
  54.     grad_tab[i].y *= m;
  55.  
  56.     if (i != 0) printf(",");
  57.     if (i!= 0 && (i % 3 ==0)) printf("\n  ");
  58.     printf("  {%10f, %10f}", grad_tab[i].x, grad_tab[i].y);
  59.   }
  60.   printf("\n};\n");
  61. }
  62.