random.c

来自「this the source code of audio compressio」· C语言 代码 · 共 45 行

C
45
字号
/***********************************************************************
*
*	RANDOM Version 49
*
***********************************************************************
*
*  Pseudo random number generator based on Knuth, Vol 2, p. 27.
*
* Function Return:
*  RANDOM - Integer variable, uniformly distributed over -32768 to 32767
*
* Warning:  This routine contains statements which are specific to VAX Fortran.
*		I'll try it anyway - Tiner
*/

#define MIDTAP 1
#define MAXTAP 4

int Rrandom ()
{
int the_random;
static short y[MAXTAP+1]={-21161, -8478, 30892,-10216, 16950};
static int j=MIDTAP, k=MAXTAP;

/*   The following is a 16 bit 2's complement addition,
*   with overflow checking disabled	*/

y[k] += y[j];

if(y[k] > 32767) /* to make 16 bit rollover to -/+ */
  y[k] = -(32768 - (y[k] & 32767));
if(y[k] < -32768)
  y[k] = y[k] & 32767;
	
the_random = y[k];
k--;
if (k < 0) k = MAXTAP;
j--;
if (j < 0) j = MAXTAP;

return(the_random);

}

⌨️ 快捷键说明

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