⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 random.cpp

📁 一个遗传算法(GA)的实现。command line 的结构。输入输出格式请见命令行
💻 CPP
字号:
/*----------------------------------------------------------------------------*/
/* random.c - contains random number generator and related utilities,         */
/* including advance_random, warmup_random, random, randomize, flip, and rnd  */
/*----------------------------------------------------------------------------*/

#include "random.h"

#include <math.h>
#include <time.h>
#include <stdlib.h>
//#include "sga.h"
#include "external.h"

extern FILE *outfp;

/* variables are declared static so that they cannot conflict with names of   */
/* other global variables in other files.  See K&R, p 80, for scope of static */
static double oldrand[55];                      /* Array of 55 random numbers */
static int jrand;                                    /* current random number */
static double rndx2;                       /* used with random normal deviate */
static int rndcalcflag;                    /* used with random normal deviate */

void advance_random()
/* Create next batch of 55 random numbers */
{
    int j1;
    double new_random;

    for(j1 = 0; j1 < 24; j1++)
    {
        new_random = oldrand[j1] - oldrand[j1+31];
        if(new_random < 0.0) new_random = new_random + 1.0;
        oldrand[j1] = new_random;
    }
    for(j1 = 24; j1 < 55; j1++)
    {
        new_random = oldrand [j1] - oldrand [j1-24];
        if(new_random < 0.0) new_random = new_random + 1.0;
        oldrand[j1] = new_random;
    }
}


/* Flip a biased coin - true if heads */
int flip(float prob)
{
    float randomperc();

    if(randomperc() <= prob)
        return(1);
    else
        return(0);
}


void initrandomnormaldeviate()
/* initialization routine for randomnormaldeviate */
{
    rndcalcflag = 1;
}


/* normal noise with specified mean & std dev: mu & sigma */
double noise(double mu ,double sigma)
{
    double randomnormaldeviate();

    return((randomnormaldeviate()*sigma) + mu);
}


void randomize()
/* Get seed number for random and start it up */
{
    float randomseed;
    int j1;


    for(j1=0; j1<=54; j1++) 
      oldrand[j1] = 0.0;

    
    jrand=0;

    if(numfiles == 0)
    {
        do
        {
            fprintf(outfp," Enter random number seed, 0.0 to 1.0 -> ");
            fscanf(infp,"%f", &randomseed);
        }
        while((randomseed < 0.0) || (randomseed > 1.0));
    }
    else
    {
        fscanf(infp,"%f", &randomseed);
		// Added by Xiaohan
		randomseed = ((unsigned)time(NULL) % 1000) / (float)1000;
		srand((unsigned)randomseed);
    }


    warmup_random(randomseed);
}


/* random normal deviate after ACM algorithm 267 / Box-Muller Method */
double randomnormaldeviate()
{
    float randomperc(); 
    double t, rndx1;

    if(rndcalcflag)
    {
        rndx1 = sqrt(- 2.0*log((double) randomperc()));
        t = 6.2831853072 * (double) randomperc();
        rndx2 = rndx1 * sin(t);
        rndcalcflag = 0;
        return(rndx1 * cos(t));
    }
    else
    {
        rndcalcflag = 1;
        return(rndx2);
    }
}


float randomperc()
/* Fetch a single random number between 0.0 and 1.0 - Subtractive Method */
/* See Knuth, D. (1969), v. 2 for details */
/* name changed from random() to avoid library conflicts on some machines*/
{
    jrand++;
    if(jrand >= 55)
    {
        jrand = 1;
        advance_random();
    }
    return((float) oldrand[jrand]);
}


/* Pick a random integer between low and high */
int rnd(int low,int high)
{
    int i;
    float randomperc();

    if(low >= high)
        i = low;
    else
    {
        i = (int)((randomperc() * (high - low + 1)) + low);
        if(i > high) i = high;
    }
    return(i);
}


/* real random number between specified limits */
float rndreal(float lo,float hi)
{
    return((randomperc() * (hi - lo)) + lo);
}


/* Get random off and running */
void warmup_random(float random_seed)
{
    int j1, ii;
    double new_random, prev_random;

    oldrand[54] = random_seed;
    new_random = 0.000000001;
    prev_random = random_seed;
    for(j1 = 1 ; j1 <= 54; j1++)
    {
        ii = (21*j1)%54;
        oldrand[ii] = new_random;
        new_random = prev_random-new_random;
        if(new_random<0.0) new_random = new_random + 1.0;
        prev_random = oldrand[ii];
    }

    advance_random();
    advance_random();
    advance_random();

    jrand = 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -