random.hpp /usr/include/gecode/support.hh Gecode::Support::LinearCongruentialGenerator Gecode Gecode::Support /*-*-mode:C++;c-basic-offset:2;indent-tabs-mode:nil-*-*/ /* *Mainauthors: *ChristianSchulte<schulte@gecode.org> *MikaelLagerkvist<lagerkvist@gecode.org> * *Copyright: *ChristianSchulte,2005 *MikaelLagerkvist,2005 * *Lastmodified: *$Date:2012-09-0711:29:57+0200(Fri,07Sep2012)$by$Author:schulte$ *$Revision:13061$ * *ThisfileispartofGecode,thegenericconstraint *developmentenvironment: *http://www.gecode.org * *Permissionisherebygranted,freeofcharge,toanypersonobtaining *acopyofthissoftwareandassociateddocumentationfiles(the *"Software"),todealintheSoftwarewithoutrestriction,including *withoutlimitationtherightstouse,copy,modify,merge,publish, *distribute,sublicense,and/orsellcopiesoftheSoftware,andto *permitpersonstowhomtheSoftwareisfurnishedtodoso,subjectto *thefollowingconditions: * *Theabovecopyrightnoticeandthispermissionnoticeshallbe *includedinallcopiesorsubstantialportionsoftheSoftware. * *THESOFTWAREISPROVIDED"ASIS",WITHOUTWARRANTYOFANYKIND, *EXPRESSORIMPLIED,INCLUDINGBUTNOTLIMITEDTOTHEWARRANTIESOF *MERCHANTABILITY,FITNESSFORAPARTICULARPURPOSEAND *NONINFRINGEMENT.INNOEVENTSHALLTHEAUTHORSORCOPYRIGHTHOLDERSBE *LIABLEFORANYCLAIM,DAMAGESOROTHERLIABILITY,WHETHERINANACTION *OFCONTRACT,TORTOROTHERWISE,ARISINGFROM,OUTOFORINCONNECTION *WITHTHESOFTWAREORTHEUSEOROTHERDEALINGSINTHESOFTWARE. * */ namespaceGecode{namespaceSupport{ template<unsignedintm,unsignedinta,unsignedintq,unsignedintr> classLinearCongruentialGenerator{ private: staticconstunsignedintmax=1UL<<31; unsignedints; unsignedintnext(void); public: voidseed(unsignedints); LinearCongruentialGenerator(unsignedints=1); unsignedintseed(void)const; unsignedintoperator ()(unsignedintn); size_tsize(void)const; }; template<unsignedintm,unsignedinta,unsignedintq,unsignedintr> forceinlineunsignedint LinearCongruentialGenerator<m,a,q,r>::next(void){ s=a*(s%q)-r*(s/q); unsignedintres=s; if(s==0)s=1; returnres; } template<unsignedintm,unsignedinta,unsignedintq,unsignedintr> forceinlinevoid LinearCongruentialGenerator<m,a,q,r>::seed(unsignedint_s){ s=_s%m; if(s==0)s=1; } template<unsignedintm,unsignedinta,unsignedintq,unsignedintr> forceinline LinearCongruentialGenerator<m,a,q,r>:: LinearCongruentialGenerator(unsignedint_s){ seed(_s); } template<unsignedintm,unsignedinta,unsignedintq,unsignedintr> forceinlineunsignedint LinearCongruentialGenerator<m,a,q,r>::seed(void)const{ returns; } template<unsignedintm,unsignedinta,unsignedintq,unsignedintr> forceinlineunsignedint LinearCongruentialGenerator<m,a,q,r>::operator ()(unsignedintn){ unsignedintx1=next()&((1<<16)-1); unsignedintx2=next()&((1<<16)-1); if(n<2)return0; doubled=static_cast<double>(((x1<<16)|x2)%max)/max; unsignedintval=static_cast<unsignedint>(n*d); return(val<n)?val:(n-1); } template<unsignedintm,unsignedinta,unsignedintq,unsignedintr> forceinlinesize_t LinearCongruentialGenerator<m,a,q,r>::size(void)const{ returnsizeof(LinearCongruentialGenerator<m,a,q,r>); } typedefLinearCongruentialGenerator<2147483647, 48271, 44488, 3399> RandomGenerator; }} //STATISTICS:support-any