utility.cpp

来自「本文件包包含了大量用VC++开发的应用程序界面」· C++ 代码 · 共 45 行

CPP
45
字号
//
//	utility.cpp. Some useful routines to make life easier
//
#include "stdafx.h"

void Randomize ()
{
time_t		t;
unsigned int	n;

/*
	Get the current time. That will be used to seed the random number
	function.
 */
	time (&t);

/*
	srand() requires an unsigned int, but time returns an unsigned long.
	Mask off any unused bits at the top in case there are any errors
	in typecasting (we want the lower part of the time because it should
	always be different, and sizeof (time_t) and sizeof (int) may not be
	the same on all systems).
 */
	n = (unsigned int) (t & ((long) ((unsigned int) -1)));

/*
		Now, use that value as the random number seed.
 */
	srand (n);
}

/*
	Return a random number in the range 0 to num-1. If 0 is passed, return
	it. If a negative number is passed, return the random number.
 */

int Random (int num)
{
	if (!num)
		return (0);
	if (num < 0)
		return (rand ());
	return (rand () % num);
}

⌨️ 快捷键说明

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