Games Animation Forum

返回   Games Animation Forum > GAF區 > 手機資訊/電腦軟硬討論

回覆
 
主題工具 顯示模式
舊 12-31-02, 04:01 PM   #1
Siren
Crazy Gamer
 
註冊日期: Dec 2001
文章: 1,178
C++ Random number problem

In my school assignment there are a part that I need to gen 2 random number (Ran1 and Ran2)

they must different each other
and I need to compare 2 random number so as to return
if Ran1 > Ran2 ----> A = 0, B = 1
else if Ran2 > Ran1 ---> A = 1, B = 0
but if I use the "rand" function
the result any time is the same

also this random function need to be execute many times in a short period (srand seems not suitable for me...)

any idea for me
many THX
Siren 目前離線   回覆時引用此篇文章
舊 01-01-03, 02:29 AM   #2
Robot-12412
Crazy Gamer
 
註冊日期: Aug 2002
文章: 1,420
1. Call srand once is enough, and use the current time as seed.

2. How short the period is? And how many times that random function needs to be called?

I try myself using rand and srand. As I'm not good at C/C++, the following is just for fun.
(The code is copied from MSDN and then adds something to it.)

代碼:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

void ThrowCoin(int *A, int *B) {
	int Ran1 = rand();
	int Ran2 = rand();

	if (Ran1 > Ran2) {
		*A = 0;
		*B = 1;
	} else {
		*A = 1;
		*B = 0;
	}
}

void main( void ) {
	int i, count, A, B;
	int head = 0, tail = 0;
	clock_t start, finish;
	double duration;

	srand( (unsigned)time( NULL ) );

	printf("Please enter number of try: ");
	scanf("%d", &count);

	start = clock();
	for( i = 0; i < count; i++ ) {
		ThrowCoin(&A, &B);
		head += A;
		tail += B;
	}
	finish = clock();
	duration = (double)(finish - start) / CLOCKS_PER_SEC;

	printf( "For %d try, there are %d heads and %d tails.\n", count, head, tail );
	printf( "%2.1f seconds used.\n", duration );
}
On my AMD TB 1GHz, 256MB computer, it takes about 13 seconds for 100 million calls, and 1.5 seconds for 10 million calls.

I think the time is reasonable. Isn't it fast enough?

With some modification (which violates the assignment's requirement), the code takes 4.6 seconds for 100 million calls, and 0.6 seconds for 10 million calls.

代碼:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

void main( void ) {
	int i, count;
	int head = 0, tail = 0;
	clock_t start, finish;
	double duration;

	srand( (unsigned)time( NULL ) );

	printf("Please enter number of try: ");
	scanf("%d", &count);

	start = clock();
	for( i = 0; i < count; i++ )
		if (rand() > rand()) head++; else tail++;
	finish = clock();
	duration = (double)(finish - start) / CLOCKS_PER_SEC;

	printf( "For %d try, there are %d heads and %d tails.\n", count, head, tail );
	printf( "%2.1f seconds used.\n", duration );
}

此篇文章於 01-01-03 02:35 AM 被 Robot-12412 編輯。
Robot-12412 目前離線   回覆時引用此篇文章
舊 01-01-03, 04:17 PM   #3
Siren
Crazy Gamer
 
註冊日期: Dec 2001
文章: 1,178
THX The problem solved

^^" I put the srand(time(0)); wrongly.
Siren 目前離線   回覆時引用此篇文章
回覆

主題工具
顯示模式

論壇跳轉

相似的主題
主題 主題作者 版面 回覆 最後發表
power supply problem? wgzc 手機資訊/電腦軟硬討論 0 08-20-03 03:58 PM
多人使用正版既XP serial number 仲算唔算正版 651132 手機資訊/電腦軟硬討論 15 08-12-03 11:21 PM
Computer Auto shut down problem, please help GimGim 手機資訊/電腦軟硬討論 2 08-12-03 10:37 AM
IC 89C51 program problem Q頭 手機資訊/電腦軟硬討論 0 05-20-03 06:20 PM
IcQ PrObLeM Q頭 手機資訊/電腦軟硬討論 3 12-30-02 09:12 PM


現在的時間是 11:44 AM


手機版 | APP版
Powered by vBulletin® 版本 3.8.3
版權所有 ©2000 - 2019,Jelsoft Enterprises Ltd. map
Games Animation Forum