如何知道rand当前的种子
用rand生成为随机数:
srand(seed);
rand();
rand();
如何得到第二个rand的种子
求助,急用,谢谢!
[解决办法]
参考C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\rand.c
/****rand.c - random number generator** Copyright (c) Microsoft Corporation. All rights reserved.**Purpose:* defines rand(), srand() - random number generator********************************************************************************/#include <cruntime.h>#include <mtdll.h>#include <stddef.h>#include <stdlib.h>/****void srand(seed) - seed the random number generator**Purpose:* Seeds the random number generator with the int given. Adapted from the* BASIC random number generator.**Entry:* unsigned seed - seed to seed rand # generator with**Exit:* None.**Exceptions:********************************************************************************/void __cdecl srand ( unsigned int seed ){ _getptd()->_holdrand = (unsigned long)seed;}/****int rand() - returns a random number**Purpose:* returns a pseudo-random number 0 through 32767.**Entry:* None.**Exit:* Returns a pseudo-random number 0 through 32767.**Exceptions:********************************************************************************/int __cdecl rand ( void ){ _ptiddata ptd = _getptd(); return( ((ptd->_holdrand = ptd->_holdrand * 214013L + 2531011L) >> 16) & 0x7fff );}
[解决办法]
第二个用的种子是 前一个种子 * 214013L+ 2531011L;
LZ仔细分析一下这句:
return( ((ptd->_holdrand = ptd->_holdrand * 214013L
+ 2531011L) >> 16) & 0x7fff );
注意里面的括号嵌套。