prand.cpp

来自「游戏编程精华01-含有几十个游戏编程例子」· C++ 代码 · 共 64 行

CPP
64
字号
/* Copyright (C) Guy W. Lecky-Thompson, 2000.  * All rights reserved worldwide. * * This software is provided "as is" without express or implied * warranties. You may freely copy and compile this source into * applications you distribute provided that the copyright text * below is included in the resulting source code, for example: * "Portions Copyright (C) Guy W. Lecky-Thompson, 2000" */// Pseudorandom number generator test harness// (C) 2000 Guy W. Lecky-Thompson#include <stdio.h>#include <stdlib.h>#include <string.h>#include "prandgen.h"pseudorandom * rand_gen;int main (int argc, char * argv[]){	long lSeed, lMax, lRand, i;	int nPad, nPerLine, nRuns, nLinePos;	// Expect the seed, maximum and runs as arguments	if (argc < 4)	{		printf("\nPrandgen <seed> <n_max> <n_runs>\n");		return -1;	}	lSeed = atol(argv[1]);	lMax = atol(argv[2]);	nPad = strlen(argv[2]); // The width of field	nRuns = atoi(argv[3]);	rand_gen = new pseudorandom(); // Create a new random number object	rand_gen->SRand(lSeed);	       // Initialise it with the seed value	nPerLine = (79-nPad) / nPad;   // Calculate number of numbers per line	nLinePos = 0;	while (nRuns > 0)	{		if (nLinePos > nPerLine)		{			nLinePos = 0;			printf("\n");		}		lRand = rand_gen->Rand(lMax);		printf("%ld",lRand);		if (lRand == 0) lRand = 1;		for (i = lRand; i < lMax; i = (i * 10)) printf(" ");		nRuns--;		nLinePos++;	}	delete rand_gen;  // Get rid of the object tidily}

⌨️ 快捷键说明

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