Path: blob/devel/elmergrid/src/metis-5.1.0/GKlib/gk_mkrandom.h
3206 views
/*!1\file2\brief Templates for portable random number generation34\date Started 5/17/075\author George6\version\verbatim $Id: gk_mkrandom.h 10711 2011-08-31 22:23:04Z karypis $ \endverbatim7*/8910#ifndef _GK_MKRANDOM_H11#define _GK_MKRANDOM_H1213/*************************************************************************/\14/*! The generator for the rand() related routines. \15\params RNGT the datatype that defines the range of values over which\16random numbers will be generated\17\params VALT the datatype that defines the contents of the array to \18be permuted by randArrayPermute() \19\params FPRFX the function prefix \20*/\21/**************************************************************************/\22#define GK_MKRANDOM(FPRFX, RNGT, VALT)\23/*************************************************************************/\24/*! Initializes the generator */ \25/**************************************************************************/\26void FPRFX ## srand(RNGT seed) \27{\28gk_randinit((uint64_t) seed);\29}\30\31\32/*************************************************************************/\33/*! Returns a random number */ \34/**************************************************************************/\35RNGT FPRFX ## rand() \36{\37if (sizeof(RNGT) <= sizeof(int32_t)) \38return (RNGT)gk_randint32(); \39else \40return (RNGT)gk_randint64(); \41}\42\43\44/*************************************************************************/\45/*! Returns a random number between [0, max) */ \46/**************************************************************************/\47RNGT FPRFX ## randInRange(RNGT max) \48{\49return (RNGT)((FPRFX ## rand())%max); \50}\51\52\53/*************************************************************************/\54/*! Randomly permutes the elements of an array p[]. \55flag == 1, p[i] = i prior to permutation, \56flag == 0, p[] is not initialized. */\57/**************************************************************************/\58void FPRFX ## randArrayPermute(RNGT n, VALT *p, RNGT nshuffles, int flag)\59{\60RNGT i, u, v;\61VALT tmp;\62\63if (flag == 1) {\64for (i=0; i<n; i++)\65p[i] = (VALT)i;\66}\67\68if (n < 10) {\69for (i=0; i<n; i++) {\70v = FPRFX ## randInRange(n);\71u = FPRFX ## randInRange(n);\72gk_SWAP(p[v], p[u], tmp);\73}\74}\75else {\76for (i=0; i<nshuffles; i++) {\77v = FPRFX ## randInRange(n-3);\78u = FPRFX ## randInRange(n-3);\79/*gk_SWAP(p[v+0], p[u+0], tmp);*/\80/*gk_SWAP(p[v+1], p[u+1], tmp);*/\81/*gk_SWAP(p[v+2], p[u+2], tmp);*/\82/*gk_SWAP(p[v+3], p[u+3], tmp);*/\83gk_SWAP(p[v+0], p[u+2], tmp);\84gk_SWAP(p[v+1], p[u+3], tmp);\85gk_SWAP(p[v+2], p[u+0], tmp);\86gk_SWAP(p[v+3], p[u+1], tmp);\87}\88}\89}\90\91\92/*************************************************************************/\93/*! Randomly permutes the elements of an array p[]. \94flag == 1, p[i] = i prior to permutation, \95flag == 0, p[] is not initialized. */\96/**************************************************************************/\97void FPRFX ## randArrayPermuteFine(RNGT n, VALT *p, int flag)\98{\99RNGT i, v;\100VALT tmp;\101\102if (flag == 1) {\103for (i=0; i<n; i++)\104p[i] = (VALT)i;\105}\106\107for (i=0; i<n; i++) {\108v = FPRFX ## randInRange(n);\109gk_SWAP(p[i], p[v], tmp);\110}\111}\112113114#define GK_MKRANDOM_PROTO(FPRFX, RNGT, VALT)\115void FPRFX ## srand(RNGT seed); \116RNGT FPRFX ## rand(); \117RNGT FPRFX ## randInRange(RNGT max); \118void FPRFX ## randArrayPermute(RNGT n, VALT *p, RNGT nshuffles, int flag);\119void FPRFX ## randArrayPermuteFine(RNGT n, VALT *p, int flag);\120121122#endif123124125