die.c
来自「与C语言四书五经之《C和指针》的配套的书上源代码。」· C语言 代码 · 共 31 行
C
31 行
/*
** Simulate the throwing of a six-sided die by returning a
** random number in the range one through six.
*/
#include <stdlib.h>
#include <stdio.h>
/*
** Compute the largest number returned by the random number
** generator that will produce a six as the value of the die.
*/
#define MAX_OK_RAND \
(int)( ( ( (long)RAND_MAX + 1 ) / 6 ) * 6 - 1 )
int
throw_die( void ){
static int is_seeded = 0;
int value;
if( !is_seeded ){
is_seeded = 1;
srand( (unsigned int)time( NULL ) );
}
do {
value = rand();
} while( value > MAX_OK_RAND );
return value % 6 + 1;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?