random.c
Go to the documentation of this file.
00001 /*<html><pre>  -<a                             href="index.htm#TOC"
00002   >-------------------------------</a><a name="TOP">-</a>
00003 
00004    random.c -- utilities
00005      Park & Miller's minimimal standard random number generator
00006      argc/argv conversion
00007 
00008      Used by rbox.  Do not use 'qh' 
00009 */
00010 
00011 #include "libqhull.h"
00012 #include <string.h>
00013 #include <stdio.h>
00014 #include <stdlib.h>
00015 
00016 #ifdef _MSC_VER  /* Microsoft Visual C++ -- warning level 4 */
00017 #pragma warning( disable : 4706)  /* assignment within conditional function */
00018 #pragma warning( disable : 4996)  /* function was declared deprecated(strcpy, localtime, etc.) */
00019 #endif
00020 
00021 /*-<a                             href="qh-globa.htm#TOC"
00022  >-------------------------------</a><a name="argv_to_command">-</a>
00023 
00024  qh_argv_to_command( argc, argv, command, max_size )
00025 
00026     build command from argc/argv
00027     max_size is at least
00028 
00029  returns:
00030     a space-delimited string of options (just as typed)
00031     returns false if max_size is too short
00032 
00033  notes:
00034     silently removes
00035     makes option string easy to input and output
00036     matches qh_argv_to_command_size()
00037 
00038     argc may be 0
00039 */
00040 int qh_argv_to_command(int argc, char *argv[], char* command, int max_size) {
00041   int i, remaining;
00042   char *s;
00043   *command= '\0';  /* max_size > 0 */
00044 
00045   if (argc) {
00046     if ((s= strrchr( argv[0], '\\')) /* get filename w/o .exe extension */
00047     || (s= strrchr( argv[0], '/')))
00048         s++;
00049     else
00050         s= argv[0];
00051     if ((int)strlen(s) < max_size)   /* WARN64 */
00052         strcpy(command, s);
00053     else
00054         goto error_argv;
00055     if ((s= strstr(command, ".EXE"))
00056     ||  (s= strstr(command, ".exe")))
00057         *s= '\0';
00058   }
00059   for (i=1; i < argc; i++) {
00060     s= argv[i];
00061     remaining= max_size - (int)strlen(command) - (int)strlen(s) - 2;   /* WARN64 */
00062     if (!*s || strchr(s, ' ')) {
00063       char *t= command + strlen(command);
00064       remaining -= 2;
00065       if (remaining < 0) {
00066         goto error_argv;
00067       }
00068       *t++= ' ';
00069       *t++= '"';
00070       while (*s) {
00071         if (*s == '"') {
00072           if (--remaining < 0)
00073             goto error_argv;
00074           *t++= '\\';
00075         }
00076         *t++= *s++;
00077       }
00078       *t++= '"';
00079       *t= '\0';
00080     }else if (remaining < 0) {
00081       goto error_argv;
00082     }else
00083       strcat(command, " ");
00084       strcat(command, s);
00085   }
00086   return 1;
00087 
00088 error_argv:
00089   return 0;
00090 } /* argv_to_command */
00091 
00092 /*-<a                             href="qh-globa.htm#TOC"
00093 >-------------------------------</a><a name="argv_to_command_size">-</a>
00094 
00095 qh_argv_to_command_size( argc, argv )
00096 
00097     return size to allocate for qh_argv_to_command()
00098 
00099 notes:
00100     argc may be 0
00101     actual size is usually shorter
00102 */
00103 int qh_argv_to_command_size(int argc, char *argv[]) {
00104     unsigned int count= 1; /* null-terminator if argc==0 */
00105     int i;
00106     char *s;
00107 
00108     for (i=0; i<argc; i++){
00109       count += (int)strlen(argv[i]) + 1;   /* WARN64 */
00110       if (i>0 && strchr(argv[i], ' ')) {
00111         count += 2;  /* quote delimiters */
00112         for (s=argv[i]; *s; s++) {
00113           if (*s == '"') {
00114             count++;
00115           }
00116         }
00117       }
00118     }
00119     return count;
00120 } /* argv_to_command_size */
00121 
00122 /*-<a                             href="qh-geom.htm#TOC"
00123   >-------------------------------</a><a name="rand">-</a>
00124 
00125   qh_rand()
00126   qh_srand( seed )
00127     generate pseudo-random number between 1 and 2^31 -2
00128 
00129   notes:
00130     For qhull and rbox, called from qh_RANDOMint(),etc. [user.h]
00131 
00132     From Park & Miller's minimal standard random number generator
00133       Communications of the ACM, 31:1192-1201, 1988.
00134     Does not use 0 or 2^31 -1
00135       this is silently enforced by qh_srand()
00136     Can make 'Rn' much faster by moving qh_rand to qh_distplane
00137 */
00138 
00139 /* Global variables and constants */
00140 
00141 int qh_rand_seed= 1;  /* define as global variable instead of using qh */
00142 
00143 #define qh_rand_a 16807
00144 #define qh_rand_m 2147483647
00145 #define qh_rand_q 127773  /* m div a */
00146 #define qh_rand_r 2836    /* m mod a */
00147 
00148 int qh_rand( void) {
00149     int lo, hi, test;
00150     int seed = qh_rand_seed;
00151 
00152     hi = seed / qh_rand_q;  /* seed div q */
00153     lo = seed % qh_rand_q;  /* seed mod q */
00154     test = qh_rand_a * lo - qh_rand_r * hi;
00155     if (test > 0)
00156         seed= test;
00157     else
00158         seed= test + qh_rand_m;
00159     qh_rand_seed= seed;
00160     /* seed = seed < qh_RANDOMmax/2 ? 0 : qh_RANDOMmax;  for testing */
00161     /* seed = qh_RANDOMmax;  for testing */
00162     return seed;
00163 } /* rand */
00164 
00165 void qh_srand( int seed) {
00166     if (seed < 1)
00167         qh_rand_seed= 1;
00168     else if (seed >= qh_rand_m)
00169         qh_rand_seed= qh_rand_m - 1;
00170     else
00171         qh_rand_seed= seed;
00172 } /* qh_srand */
00173 
00174 /*-<a                             href="qh-geom.htm#TOC"
00175 >-------------------------------</a><a name="randomfactor">-</a>
00176 
00177 qh_randomfactor( scale, offset )
00178 return a random factor r * scale + offset
00179 
00180 notes:
00181 qh.RANDOMa/b are defined in global.c
00182 */
00183 realT qh_randomfactor(realT scale, realT offset) {
00184     realT randr;
00185 
00186     randr= qh_RANDOMint;
00187     return randr * scale + offset;
00188 } /* randomfactor */
00189 
00190 /*-<a                             href="qh-geom.htm#TOC"
00191 >-------------------------------</a><a name="randommatrix">-</a>
00192 
00193 qh_randommatrix( buffer, dim, rows )
00194 generate a random dim X dim matrix in range [-1,1]
00195 assumes buffer is [dim+1, dim]
00196 
00197 returns:
00198 sets buffer to random numbers
00199 sets rows to rows of buffer
00200 sets row[dim] as scratch row
00201 */
00202 void qh_randommatrix(realT *buffer, int dim, realT **rows) {
00203     int i, k;
00204     realT **rowi, *coord, realr;
00205 
00206     coord= buffer;
00207     rowi= rows;
00208     for (i=0; i < dim; i++) {
00209         *(rowi++)= coord;
00210         for (k=0; k < dim; k++) {
00211             realr= qh_RANDOMint;
00212             *(coord++)= 2.0 * realr/(qh_RANDOMmax+1) - 1.0;
00213         }
00214     }
00215     *rowi= coord;
00216 } /* randommatrix */
00217 
00218 /*-<a                             href="qh-globa.htm#TOC"
00219   >-------------------------------</a><a name="strtol">-</a>
00220 
00221   qh_strtol( s, endp) qh_strtod( s, endp)
00222     internal versions of strtol() and strtod()
00223     does not skip trailing spaces
00224   notes:
00225     some implementations of strtol()/strtod() skip trailing spaces
00226 */
00227 double qh_strtod(const char *s, char **endp) {
00228   double result;
00229 
00230   result= strtod(s, endp);
00231   if (s < (*endp) && (*endp)[-1] == ' ')
00232     (*endp)--;
00233   return result;
00234 } /* strtod */
00235 
00236 int qh_strtol(const char *s, char **endp) {
00237   int result;
00238 
00239   result= (int) strtol(s, endp, 10);     /* WARN64 */
00240   if (s< (*endp) && (*endp)[-1] == ' ')
00241     (*endp)--;
00242   return result;
00243 } /* strtol */


libqhull-ours
Author(s): Robert Krug
autogenerated on Mon Jan 6 2014 11:32:11