`
sjgau
  • 浏览: 95540 次
  • 性别: Icon_minigender_1
  • 来自: 台灣省 台北市
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

VC++6.0 關於 srand() 提供的範例

阅读更多
以下的關於 srand() ,原廠所提供的範例,
可以提供一些有用的資訊。

srand() 是用來設定接下來的 系統所提供的 亂數產生器
所需要的 種子數。

如果不加以設定的話,系統所產生的亂數是固定的,
可以被預測到。

使用 srand(time(NULL)); 之後,系統會抓取現在的時間的秒數,
這個秒數,是從 1970.01.01 的凌晨 00:00:00 到現在所經過的全部
的秒數。

所以,如果在 同一個秒數之內,兩次呼叫 srand();
所產生的一系列亂數,會完全一樣。

避免的方法是,不要採用 time(NULL);

詳細的想法,容後再稟。


/* RAND.C: This program seeds the random-number generator
* with the time, then displays 10 random integers.
*/

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

void main( void )
{
	int i;
	
	/* Seed the random-number generator with current time so that
    * the numbers will be different every time we run.
    */
	srand( (unsigned)time( NULL ) );
	
	/* Display 10 numbers. */
	for( i = 0;   i < 10;i++ )
		printf( "  %6d\n", rand() );
}


0
0
分享到:
评论
1 楼 sjgau 2010-06-17  
即使在不同的 秒數,或是 微秒(ms)數,
如果相鄰的時間很接近,那麼所產生的隨機數列的開始的值,
也會很接近。

解決的方法,容後再稟。

相关推荐

Global site tag (gtag.js) - Google Analytics