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

📄 knuthrandom.java

📁 基于人工免疫原理的入侵检测系统框架
💻 JAVA
字号:
/*================= * Copyright (C) 2001  Steven Hofmeyr * * Lisys is a program that monitors TCP SYN packets to detect network * traffic anomalies. * * Licensed under the GNU General Public License (GPL), version 2 or * higher.  Please see the COPYING and PATENT files included with the * Lisys distribution, which can be found at: * *   http://www.cs.unm.edu/~judd/lisys/ * * Also, the current text of the GPL can be found at:  * *   http://www.gnu.org/copyleft/gpl.html *  * Note that Lisys has NO WARRANTY! *=================*/package edu.unm.cs.lisys.util;import edu.unm.cs.lisys.debug.*;import java.io.*;import java.lang.Math;/**========== * KnuthRandom.java *   This algorithm is based on the code on page 283 of "Numerical *   Recipes in C" * * Here are the people who have worked on this code in the order they * have worked on it: *   @author Steven Hofmeyr <sah@santafe.edu> *   @author Justin Balthrop <judd@cs.unm.edu> *==========*/public class KnuthRandom implements Serializable {    private static final long MBIG = 1000000000;    private static final long MSEED = 161803398;    private static final double FAC = (1.0 / MBIG);        private int inext;    private int inextp;    private long[] ma = new long[56];        public KnuthRandom(long rseed) { initializeRandom(rseed); }        /**==========     * initializeRandom:     *   This initializes the random seed. If we feed in a negative     *   number, then time is used to initialize the seed.     *     * @param seed the seed     * @return the seed that was used     *==========*/    private long initializeRandom(long seed)     {	long mj, mk;	if (seed < 0) 	    seed = System.currentTimeMillis() % MBIG;		if (seed >= MBIG) {	    Debug.exception(this, new KnuthRandomException		("Seed value too large (> " + MBIG + ")"+ seed));	    System.exit(-1);	}		mj = Math.abs(MSEED-Math.abs(seed));	mj = mj % MBIG;	ma[55] = mj;	mk = 1;		for (int i = 1; i <= 54; i++) {	    int j = (21 * i) % 55;	    ma[j] = mk;	    mk = mj - mk;	    if (mk < 0) mk += MBIG;	    mj = ma[j];	}		for (int k = 0; k < 4; k++) {	    for (int i = 1; i <= 55; i++) {		ma[i] -= ma[1 + (i + 30) % 55];		if (ma[i] < 0) ma[i] += MBIG;	    }	}		inext = 0;	inextp = 31;		return seed;    }        /**==========     * fraction:     *     * @return a random double between 0 and 1     *==========*/    public double fraction()    {	long mj;	if (++inext == 56) inext = 1;	if (++inextp == 56) inextp = 1;	mj = ma[inext] - ma[inextp];	if (mj < 0) mj += MBIG;	ma[inext] = mj;	return mj * FAC;    }        /**==========     * intRange:     *     * @param range one more than the largest random number possible     * @return an integer between 0 and range - 1 inclusive     *==========*/    public int intRange(int range)     {	int retval = (int)(fraction() * range);	if (retval < 0 || retval >= range) {	    // This should never happen.	    Debug.exception(this, new KnuthRandomException("Out of bounds."));	    System.exit(1);	}	return retval;    }          /**==========     * inBetween:     *     * @param start the smallest random number possible     * @param finish the largest random number possible     * @return an integer between start and finish     *==========*/    public int intBetween(int start, int finish) {	int spread = (finish + 1) - start;	return intRange(spread) + start;    }}

⌨️ 快捷键说明

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