rand.c

来自「基于4个mips核的noc设计」· C语言 代码 · 共 45 行

C
45
字号
/* * libc/stdlib/rand.c * ANSI/ISO 9899-1990, Sections 7.10.2.1 and 7.10.2.2. * * int rand(void) * Return a random number in the range [0, RAND_MAX]. * * void srand(unsigned int seed) * Seed the random number generator with the given value. * * This code uses the portable implementation from the * example given in Section 4.10.2.2 of the Standard. */#include <stdlib.h>static unsigned long int rand_flag, rand_next;int rand(void){	unsigned long int *nextp;	/*	 * ANSI 7.10.2.2:	 * If rand is called before any calls to srand have been made,	 * the same sequence shall be generated as when srand is first called	 * with a seed value of 1.	 */	if (rand_flag == 0)		srand(1);	nextp = &rand_next;	*nextp = (*nextp) * 1103515245 + 12345;	return (unsigned int)((*nextp)/65536) % 32768;}voidsrand(unsigned int seed){	rand_flag = 1;	rand_next = seed;}/* end of rand.c */

⌨️ 快捷键说明

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