#include /* Copyright (c) 1981 Gary Perlman This software may be copied freely provided: (1) it is not used for personal or material gain, and (2) this notice accompanies each copy. Disclaimer: No guarantees of performance accompany this software, nor is any responsbility assumed on the part of the author. All the software has been tested extensively and every effort has been made to insure its reliability.*/ #ifndef DIVISOR #define DIVISOR (sizeof(int)==4?2147483648.0:32768.0) #endif double divisor =DIVISOR; /* the divisor is used to map integers returned by random() * to between zero and one. It is one larger than the * largest integer on a machine */ static unseeded = 1; long time (); #define MAXWIDTH 20 /* permut randomizes the order of the elements of array. * array is the base address of the elts to be permuted, * nelem is the number of elements to be permuted, and * width is the size of one array element, so a normal call * would be: * permut (order, nstim, sizeof (*order)); */ int permut (char *array, int nelem, int width) { char temp[MAXWIDTH]; register j; char *aptr, *rptr; int i; if (nelem <= 1 || width < 1) return (1); if (unseeded) srandom ((int) time (unseeded = 0)); for (i = 0; i < nelem-1; i++) { aptr = array + i*width; rptr = aptr + ((int) ((random() / divisor) * (nelem - i)))*width; if (aptr != rptr) for (j = 0; j < width; j++) { temp[j] = aptr[j]; aptr[j] = rptr[j]; rptr[j] = temp[j]; } } return (0); }