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

📄 random.h

📁 随机数生成源代码
💻 H
📖 第 1 页 / 共 2 页
字号:
	virtual ~bi_random_generator() { }

	inline const unsigned long int NDIV() {
		const unsigned long int MSHIFT = 1 + (psr_1.M()>>LOG2_TABLE_SIZE());
		return(MSHIFT);
	}
	inline const unsigned long int TABLE_CONTRACTION() { return(y/NDIV()); }


private:

	psuedoR21(T) psr_1;
	psuedoR22(T) psr_2;

	virtual void init_generator(unsigned long int *s)
		{
			if ( buffer == NULL ) buffer = new unsigned long int (TABLE_SIZE() + 10);
			if ( buffer == NULL ) throw "Bad Allocation";

			if ( *s == 0 ) *s = 1;
			for ( unsigned int j = TABLE_SIZE() + 7; j > 0; j-- ) {
				unsigned long int i = psr_1.i_value();
				if ( j < TABLE_SIZE() ) buffer[j] = i;
			}
			y = buffer[0];
		}


public:

	void reinit(unsigned long int i) { i0 = i; init_generator(s0); }

	inline T value() { // not using the mask.

		unsigned long int i = psr_1.i_value();
		unsigned long int i2 = psr_2.i_value();

		unsigned long int j = TABLE_CONTRACTION();

		long int y_hat = buffer[j] - i2;
		buffer[j] = i;

		if ( y_hat < 1 ) y_hat += psr_1.M();
		y = y_hat;

		T result = y*psr_1.SAMPLE();
		if ( result > RNMX() ) result = RNMX();

		return(result);
	}

	inline T pick_a_number_between_bounds() {
		T result = value();
		return(shift(result));
	}

};



#define rgen2(T) bi_random_generator<T, shuffle_constants<T> >



template<class T>
class constants_qd_32 {
public:
	constants_qd_32() {}

	inline const unsigned long int M() { return(710425); }
	inline const unsigned long int A() { return(4096); }
	inline const unsigned long int C() { return(150889); }
};


//
// Quick and Dirty
//

template<class T, class C>
class qd_random_generator : public generator, public C, public generator_parameters<T>  {
public:

	qd_random_generator() : generator() {
	}

	qd_random_generator(int i) : generator(i) {
	}

	virtual ~qd_random_generator() { }


private:

	virtual void init_generator(unsigned long int *s)
		{
		}


public:

	void reinit(unsigned long int i) { i0 = i; }

	inline T value() { // not using the mask.
		i0 = (i0*A() + C()) % (M());
		result = (T(i0))/M();
		return(result);
	}

	inline T pick_a_number_between_bounds() {
		T result = value();
		return(shift(result));
	}

};


//
//
//

class poly_32 {
public:
	poly_32() {}
	poly_32(short int *p_coefs) {} // This one does nothing.

	inline const unsigned long int a18() { return(1<<17); }
	inline const unsigned long int a5() { return(16); }
	inline const unsigned long int a2() { return(2); }
	inline const unsigned long int a1() { return(1); }

	inline const unsigned long int MASK() { return(a1() + a2() + a5()); }
	inline const unsigned long int ORDER() { return(a18()); }

	virtual void init_mask() {}
};

class word_size_poly {
public:
	word_size_poly() { coeficients = NULL; }
	word_size_poly(short int *p_coefs) { coeficients = p_coefs; } // This one does nothing.

	short int *coeficients;
	unsigned long int order;
	unsigned long int mask;

	inline const unsigned long int a1() { return(1); }

	inline const unsigned long int MASK() { return(mask); }
	inline const unsigned long int ORDER() { return(order); }

	virtual void init_mask() {
		if ( coeficients == NULL ) throw "Coeficient not specified in word size poly.";
		order = coeficients[1];
		if ( order > 32 ) throw "Polynomial order > 32";
		order = 1<<(order-1);

		int n = coeficients[0];
		mask = 0;
		for ( int i = 2; i <= n; i++ ) {
			mask += (1<<(coeficients[i]-1));
		}
	}

	virtual void change_polynomial(short int *p_coefs) {
		coeficients = p_coefs;
		init_mask();
	}
};

//
// bit_generator
// in the template, P is a primitive polynomial.
// Or it is able to produce a mask based on one that it is assigned.
//
// When the polynomial descriptor is given, it is assumed to contain
// the number of coeficients in the first short word. Also, it is assumed
// that the polynomial is arranged from highest to lowest order.
//
template<class P>
class bit_generator : public generator, public P  {

public:

	bit_generator() {
	}

	bit_generator(unsigned long int i) : generator(i) {
	}

	// initialize with the polynomial descriptor.
	bit_generator(short int *p_coefs) : P(p_coefs) {
		if ( p_coefs == NULL ) { throw "NULL Polynomial"; }
	}

	// initialize with both a seed and the polynomial descriptor.
	bit_generator(unsigned long int i,short int *p_coefs) : generator(i), P(p_coefs) {
		if ( p_coefs == NULL ) { throw "NULL Polynomial"; }
	}


	virtual ~bit_generator() {}

	virtual void init_generator(unsigned long int *s)  {
		// Call the mask initializer if it is there.
		init_mask();
	}

	void reinit(unsigned long int i) { i0 = i; }


	inline unsigned short m_value() { // Using the mask
		unsigned short result;
		unsigned long  i = i0;
	//-----
		if ( ORDER() & i0 ) {
			i = (i ^ MASK()) << 1) | a0();
			result = 1;
		} else {
			i <<= 1;
			result = 0;
		}
		i0 = i;
	//-----
		return(result);
	}

};



class big_size_poly {
public:
	big_size_poly() { coeficients = NULL; }
	big_size_poly(short int *p_coefs) { coeficients = p_coefs; } // This one does nothing.

	short int *coeficients;
	unsigned long int order;
	unsigned long int *mask;
	unsigned short _N;

	inline const unsigned long int a1() { return(1); }

	inline const unsigned long int MASK(int index) { return(mask[index]); }
	inline const unsigned long int ORDER() { return(order); }
	inline const unsigned short int N() { return(_N); }

	virtual void init_mask() {
		if ( coeficients == NULL ) throw "Coeficient not specified in word size poly.";
		int i_order = coeficients[1];

		int bits_per_word = (sizeof(unsigned long int)*8);
		if ( i_order <= bits_per_word ) throw "This polynomial order is more efficient in a word size poly";

		int n_words = i_order / bits_per_word;
		_N = n_words;
		mask =  new unsigned long int ( n_words );

		int n = coeficients[0]; // Usually not many coeficients.
		i_order = coeficients[1] - 1;

		for ( int i = 2; i <= n; i++ ) {
			unsigned long int c = coeficients[i]-1;
			unsigned long int B = (i_order - c)/bits_per_word;

			while ( c > bits_per_word ) c -= bits_per_word;
			unsigned long int M = 1<<c;

			mask[B] += M;
		}

		while ( i_order > bits_per_word ) i_order -= bits_per_word;
		order = 1 << i_order;
	}

	virtual void change_polynomial(short int *p_coefs) {
		coeficients = p_coefs;
		init_mask();
	}

};

//
// bit_generator
// in the template, P is a primitive polynomial.
// Or it is able to produce a mask based on one that it is assigned.
//
// When the polynomial descriptor is given, it is assumed to contain
// the number of coeficients in the first short word. Also, it is assumed
// that the polynomial is arranged from highest to lowest order.
//
template<class P>
class bit_array_generator : public generator, public P  {

public:

	bit_array_generator(unsigned long int *i_ptr) : generator() {
		gen_buffer = *i_ptr;
	}

	// initialize with the polynomial descriptor.
	bit_array_generator(unsigned long int *i_ptr, short int *p_coefs) : P(p_coefs), generator() {
		gen_buffer = *i_ptr;
		if ( p_coefs == NULL ) { throw "NULL Polynomial"; }
	}

	virtual ~bit_array_generator() {}

	virtual void init_generator(unsigned long int *s)  {
		// Call the mask initializer if it is there.
		init_mask();
	}

	void reinit(unsigned long int *i_ptr) { gen_buffer = i_ptr; }
	unsigned long int *gen_buffer; // The gen_buffer


	unsigned long int mask_shift(unsigned long int *gen_buffer, unsigned char bit) {
		//
		unsigned short int n = N();
		const unsigned long int top_bit = (1<<31);
		//

		unsigned char h_bit = bit;
		for ( int i = n-1; i >= 0; i-- ) {
			int j = gen_buffer[i];
			unsigned char hold_bit = ((j & top_bit) >> 31 ) & 0x1;

			j <<= 1;
			j |= h_bit;
			gen_buffer[i] = j;

			h_bit = hold_bit;
		}


		return(gen_buffer[0]);
	}


	inline unsigned short m_value() { // Using the mask
		unsigned short result;
		unsigned long  i = i0;  // i0 now holds the highest mask word.
	//-----
		if ( ORDER() & i0 ) {
			i = mask_shift(gen_buffer,(unsigned char)a0()); // return the highest word
			result = 1;
		} else {
			i = mask_shift(gen_buffer,0);
			result = 0;
		}
		i0 = i;
	//-----
		return(result);
	}

};


#endif

⌨️ 快捷键说明

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