📄 random.cc
字号:
/*************************************************************************** Random.cc - description ------------------- begin : Fri Nov 10 2000 copyright : (C) 2000 by Christian Blum email : cblum@ulb.ac.be ***************************************************************************//*************************************************************************** Program's name: hc-mmas-ubqp Ant Colony Optimization algorithm to tackle Unary Binary Quadratic Programming Copyright (C) 2003 Christian Blum This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Author's contact details: email: cblum@ulb.ac.be mail address: Universite Libre de Bruxelles, IRIDIA, Av. Franklin Roosevelt 50, CP 194/6, B-1050 Brussels, Belgium ***************************************************************************/#include "Random.h"#include <stdio.h>#define VERBOSE(x) x#define VERYVERBOSE(x)/* pseudo-random number generator as proposed in numerical recipes in C Input: a long value; has to be the seed variable Output: a pseudo-random number uniformly distributed in [0,1] Side effects: changes the value of the input variable, must be this way*/double Random::ran01( long *idum ){ long k; double ans; k =(*idum)/IQ; *idum = IA * (*idum - k * IQ) - IR * k; if (*idum < 0 ) *idum += IM; ans = AM * (*idum); return ans;}double Random::gasdev(long *idum){ //double ran01(long *idum); static int iset=0; static double gset; double fac,rsq,v1,v2; if (iset == 0) { do { v1=2.0*ran01(idum)-1.0; v2=2.0*ran01(idum)-1.0; //v1 = ran01(idum); //v2 = ran01(idum); rsq=v1*v1+v2*v2; } while (rsq >= 1.0 || rsq == 0.0); fac=sqrt(-2.0*log(rsq)/rsq); gset=v1*fac; iset=1; return v2*fac; } else { iset=0; return gset; }}long int* Random::generate_array(const int& size) { long int i, j, help; long int *v; v = (long int*) malloc( size * sizeof(long int) ); for ( i = 0 ; i < size; i++ ) v[i] = i; for ( i = 0 ; i < size-1 ; i++) { j = (long int) ( ran01( &seed ) * (size - i)); assert( i + j < size ); help = v[i]; v[i] = v[i+j]; v[i+j] = help; } VERYVERBOSE ( printf("Random vector:\n"); for (i = 0 ; i < size ; i++ ) printf(" %ld ",v[i]); printf("\n"); ) return v;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -