随机函数的定义和使用到的DS和函数:
1. 实现在rand.c文件中:

Code
1 /***
2 *rand.c - random number generator
3 *
4 * Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
5 *
6 *Purpose:
7 * defines rand(), srand() - random number generator
8 *
9 *******************************************************************************/
10
11 #include <cruntime.h>
12 #include <mtdll.h>
13 #include <stddef.h>
14 #include <stdlib.h>
15
16 #ifndef _MT
17 static long holdrand = 1L;
18 #endif /* _MT */
19
20 /***
21 *void srand(seed) - seed the random number generator
22 *
23 *Purpose:
24 * Seeds the random number generator with the int given. Adapted from the
25 * BASIC random number generator.
26 *
27 *Entry:
28 * unsigned seed - seed to seed rand # generator with
29 *
30 *Exit:
31 * None.
32 *
33 *Exceptions:
34 *
35 *******************************************************************************/
36
37 void __cdecl srand (
38 unsigned int seed
39 )
40 {
41 #ifdef _MT
42
43 _getptd()->_holdrand = (unsigned long)seed;
44
45 #else /* _MT */
46 holdrand = (long)seed;
47 #endif /* _MT */
48 }
49
50
51 /***
52 *int rand() - returns a random number
53 *
54 *Purpose:
55 * returns a pseudo-random number 0 through 32767.
56 *
57 *Entry:
58 * None.
59 *
60 *Exit:
61 * Returns a pseudo-random number 0 through 32767.
62 *
63 *Exceptions:
64 *
65 *******************************************************************************/
66
67 int __cdecl rand (
68 void
69 )
70 {
71 #ifdef _MT
72
73 _ptiddata ptd = _getptd();
74
75 return( ((ptd->_holdrand = ptd->_holdrand * 214013L
76 + 2531011L) >> 16) & 0x7fff );
77
78 #else /* _MT */
79 return(((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff);
80 #endif /* _MT */
81 }
82
2. 用到了一个DS定义在MTDLL.H头文件中:

Code
1 /* Structure for each thread\'s data */
2
3 struct _tiddata {
4 unsigned long _tid; /* thread ID */
5
6
7 unsigned long _thandle; /* thread handle */
8
9 int _terrno; /* errno value */
10 unsigned long _tdoserrno; /* _doserrno value */
11 unsigned int _fpds; /* Floating Point data segment */
12 unsigned long _holdrand; /* rand() seed value */
13 char * _token; /* ptr to strtok() token */
14 #ifdef _WIN32
15 wchar_t * _wtoken; /* ptr to wcstok() token */
16 #endif /* _WIN32 */
17 unsigned char * _mtoken; /* ptr to _mbstok() token */
18
19 /* following pointers get malloc\'d at runtime */
20 char * _errmsg; /* ptr to strerror()/_strerror() buff */
21 char * _namebuf0; /* ptr to tmpnam() buffer */
22 #ifdef _WIN32
23 wchar_t * _wnamebuf0; /* ptr to _wtmpnam() buffer */
24 #endif /* _WIN32 */
25 char * _namebuf1; /* ptr to tmpfile() buffer */
26 #ifdef _WIN32
27 wchar_t * _wnamebuf1; /* ptr to _wtmpfile() buffer */
28 #endif /* _WIN32 */
29 char * _asctimebuf; /* ptr to asctime() buffer */
30 #ifdef _WIN32
31 wchar_t * _wasctimebuf; /* ptr to _wasctime() buffer */
32 #endif /* _WIN32 */
33 void * _gmtimebuf; /* ptr to gmtime() structure */
34 char * _cvtbuf; /* ptr to ecvt()/fcvt buffer */
35
36 /* following fields are needed by _beginthread code */
37 void * _initaddr; /* initial user thread address */
38 void * _initarg; /* initial user thread argument */
39
40 /* following three fields are needed to support signal handling and
41 * runtime errors */
42 void * _pxcptacttab; /* ptr to exception-action table */
43 void * _tpxcptinfoptrs; /* ptr to exception info pointers */
44 int _tfpecode; /* float point exception code */
45
46 /* following field is needed by NLG routines */
47 unsigned long _NLG_dwCode;
48
49 /*
50 * Per-Thread data needed by C++ Exception Handling
51 */
52 void * _terminate; /* terminate() routine */
53 void * _unexpected; /* unexpected() routine */
54 void * _translator; /* S.E. translator */
55 void * _curexception; /* current exception */
56 void * _curcontext; /* current exception context */
57 #if defined (_M_MRX000)
58 void * _pFrameInfoChain;
59 void * _pUnwindContext;
60 void * _pExitContext;
61 int _MipsPtdDelta;
62 int _MipsPtdEpsilon;
63 #elif defined (_M_PPC)
64 void * _pExitContext;
65 void * _pUnwindContext;
66 void * _pFrameInfoChain;
67 int _FrameInfo[6];
68 #endif /* defined (_M_PPC) */
69 };
70
71 typedef struct _tiddata * _ptiddata;
3. 用到了一个function实现在tidtable.c文件中:

Code
1 /***
2 *_ptiddata _getptd(void) - get per-thread data structure for the current thread
3 *
4 *Purpose:
5 *
6 *Entry:
7 * unsigned long tid
8 *
9 *Exit:
10 * success = pointer to _tiddata structure for the thread
11 * failure = fatal runtime exit
12 *
13 *Exceptions:
14 *
15 *******************************************************************************/
16
17 _ptiddata __cdecl _getptd (
18 void
19 )
20 {
21 _ptiddata ptd;
22 DWORD TL_LastError;
23
24
25 TL_LastError = GetLastError();
26 if ( (ptd = TlsGetValue(__tlsindex)) == NULL ) {
27 /*
28 * no per-thread data structure for this thread. try to create
29 * one.
30 */
31 if ( ((ptd = _calloc_crt(1, sizeof(struct _tiddata))) != NULL) &&
32 TlsSetValue(__tlsindex, (LPVOID)ptd) ) {
33
34 /*
35 * Initialize of per-thread data
36 */
37
38 _initptd(ptd);
39
40 ptd->_tid = GetCurrentThreadId();
41 ptd->_thandle = (unsigned long)(-1L);
42 }
43 else
44 _amsg_exit(_RT_THREAD); /* write message and die */
45 }
46
47 SetLastError(TL_LastError);
48
49
50 return(ptd);
51 }
/***
2 *_ptiddata _getptd(void) - get per-thread data structure for the current thread
3 *
4 *Purpose:
5 *
6 *Entry:
7 * unsigned long tid
8 *
9 *Exit:
10 * success = pointer to _tiddata structure for the thread
11 * failure = fatal runtime exit
12 *
13 *Exceptions:
14 *
15 *******************************************************************************/
16
17 _ptiddata __cdecl _getptd (
18 void
19 )
20 {
21 _ptiddata ptd;
22 DWORD TL_LastError;
23
24
25 TL_LastError = GetLastError();
26 if ( (ptd = TlsGetValue(__tlsindex)) == NULL ) {
27 /*
28 * no per-thread data structure for this thread. try to create
29 * one.
30 */
31 if ( ((ptd = _calloc_crt(1, sizeof(struct _tiddata))) != NULL) &&
32 TlsSetValue(__tlsindex, (LPVOID)ptd) ) {
33
34 /*
35 * Initialize of per-thread data
36 */
37
38 _initptd(ptd);
39
40 ptd->_tid = GetCurrentThreadId();
41 ptd->_thandle = (unsigned long)(-1L);
42 }
43 else
44 _amsg_exit(_RT_THREAD); /* write message and die */
45 }
46
47 SetLastError(TL_LastError);
48
49
50
1. 实现在rand.c文件中:
1 /***
2 *rand.c - random number generator
3 *
4 * Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
5 *
6 *Purpose:
7 * defines rand(), srand() - random number generator
8 *
9 *******************************************************************************/
10
11 #include <cruntime.h>
12 #include <mtdll.h>
13 #include <stddef.h>
14 #include <stdlib.h>
15
16 #ifndef _MT
17 static long holdrand = 1L;
18 #endif /* _MT */
19
20 /***
21 *void srand(seed) - seed the random number generator
22 *
23 *Purpose:
24 * Seeds the random number generator with the int given. Adapted from the
25 * BASIC random number generator.
26 *
27 *Entry:
28 * unsigned seed - seed to seed rand # generator with
29 *
30 *Exit:
31 * None.
32 *
33 *Exceptions:
34 *
35 *******************************************************************************/
36
37 void __cdecl srand (
38 unsigned int seed
39 )
40 {
41 #ifdef _MT
42
43 _getptd()->_holdrand = (unsigned long)seed;
44
45 #else /* _MT */
46 holdrand = (long)seed;
47 #endif /* _MT */
48 }
49
50
51 /***
52 *int rand() - returns a random number
53 *
54 *Purpose:
55 * returns a pseudo-random number 0 through 32767.
56 *
57 *Entry:
58 * None.
59 *
60 *Exit:
61 * Returns a pseudo-random number 0 through 32767.
62 *
63 *Exceptions:
64 *
65 *******************************************************************************/
66
67 int __cdecl rand (
68 void
69 )
70 {
71 #ifdef _MT
72
73 _ptiddata ptd = _getptd();
74
75 return( ((ptd->_holdrand = ptd->_holdrand * 214013L
76 + 2531011L) >> 16) & 0x7fff );
77
78 #else /* _MT */
79 return(((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff);
80 #endif /* _MT */
81 }
82
2. 用到了一个DS定义在MTDLL.H头文件中:
1 /* Structure for each thread\'s data */
2
3 struct _tiddata {
4 unsigned long _tid; /* thread ID */
5
6
7 unsigned long _thandle; /* thread handle */
8
9 int _terrno; /* errno value */
10 unsigned long _tdoserrno; /* _doserrno value */
11 unsigned int _fpds; /* Floating Point data segment */
12 unsigned long _holdrand; /* rand() seed value */
13 char * _token; /* ptr to strtok() token */
14 #ifdef _WIN32
15 wchar_t * _wtoken; /* ptr to wcstok() token */
16 #endif /* _WIN32 */
17 unsigned char * _mtoken; /* ptr to _mbstok() token */
18
19 /* following pointers get malloc\'d at runtime */
20 char * _errmsg; /* ptr to strerror()/_strerror() buff */
21 char * _namebuf0; /* ptr to tmpnam() buffer */
22 #ifdef _WIN32
23 wchar_t * _wnamebuf0; /* ptr to _wtmpnam() buffer */
24 #endif /* _WIN32 */
25 char * _namebuf1; /* ptr to tmpfile() buffer */
26 #ifdef _WIN32
27 wchar_t * _wnamebuf1; /* ptr to _wtmpfile() buffer */
28 #endif /* _WIN32 */
29 char * _asctimebuf; /* ptr to asctime() buffer */
30 #ifdef _WIN32
31 wchar_t * _wasctimebuf; /* ptr to _wasctime() buffer */
32 #endif /* _WIN32 */
33 void * _gmtimebuf; /* ptr to gmtime() structure */
34 char * _cvtbuf; /* ptr to ecvt()/fcvt buffer */
35
36 /* following fields are needed by _beginthread code */
37 void * _initaddr; /* initial user thread address */
38 void * _initarg; /* initial user thread argument */
39
40 /* following three fields are needed to support signal handling and
41 * runtime errors */
42 void * _pxcptacttab; /* ptr to exception-action table */
43 void * _tpxcptinfoptrs; /* ptr to exception info pointers */
44 int _tfpecode; /* float point exception code */
45
46 /* following field is needed by NLG routines */
47 unsigned long _NLG_dwCode;
48
49 /*
50 * Per-Thread data needed by C++ Exception Handling
51 */
52 void * _terminate; /* terminate() routine */
53 void * _unexpected; /* unexpected() routine */
54 void * _translator; /* S.E. translator */
55 void * _curexception; /* current exception */
56 void * _curcontext; /* current exception context */
57 #if defined (_M_MRX000)
58 void * _pFrameInfoChain;
59 void * _pUnwindContext;
60 void * _pExitContext;
61 int _MipsPtdDelta;
62 int _MipsPtdEpsilon;
63 #elif defined (_M_PPC)
64 void * _pExitContext;
65 void * _pUnwindContext;
66 void * _pFrameInfoChain;
67 int _FrameInfo[6];
68 #endif /* defined (_M_PPC) */
69 };
70
71 typedef struct _tiddata * _ptiddata;
3. 用到了一个function实现在tidtable.c文件中:
1 /***
2 *_ptiddata _getptd(void) - get per-thread data structure for the current thread
3 *
4 *Purpose:
5 *
6 *Entry:
7 * unsigned long tid
8 *
9 *Exit:
10 * success = pointer to _tiddata structure for the thread
11 * failure = fatal runtime exit
12 *
13 *Exceptions:
14 *
15 *******************************************************************************/
16
17 _ptiddata __cdecl _getptd (
18 void
19 )
20 {
21 _ptiddata ptd;
22 DWORD TL_LastError;
23
24
25 TL_LastError = GetLastError();
26 if ( (ptd = TlsGetValue(__tlsindex)) == NULL ) {
27 /*
28 * no per-thread data structure for this thread. try to create
29 * one.
30 */
31 if ( ((ptd = _calloc_crt(1, sizeof(struct _tiddata))) != NULL) &&
32 TlsSetValue(__tlsindex, (LPVOID)ptd) ) {
33
34 /*
35 * Initialize of per-thread data
36 */
37
38 _initptd(ptd);
39
40 ptd->_tid = GetCurrentThreadId();
41 ptd->_thandle = (unsigned long)(-1L);
42 }
43 else
44 _amsg_exit(_RT_THREAD); /* write message and die */
45 }
46
47 SetLastError(TL_LastError);
48
49
50 return(ptd);
51 }
/***
2 *_ptiddata _getptd(void) - get per-thread data structure for the current thread
3 *
4 *Purpose:
5 *
6 *Entry:
7 * unsigned long tid
8 *
9 *Exit:
10 * success = pointer to _tiddata structure for the thread
11 * failure = fatal runtime exit
12 *
13 *Exceptions:
14 *
15 *******************************************************************************/
16
17 _ptiddata __cdecl _getptd (
18 void
19 )
20 {
21 _ptiddata ptd;
22 DWORD TL_LastError;
23
24
25 TL_LastError = GetLastError();
26 if ( (ptd = TlsGetValue(__tlsindex)) == NULL ) {
27 /*
28 * no per-thread data structure for this thread. try to create
29 * one.
30 */
31 if ( ((ptd = _calloc_crt(1, sizeof(struct _tiddata))) != NULL) &&
32 TlsSetValue(__tlsindex, (LPVOID)ptd) ) {
33
34 /*
35 * Initialize of per-thread data
36 */
37
38 _initptd(ptd);
39
40 ptd->_tid = GetCurrentThreadId();
41 ptd->_thandle = (unsigned long)(-1L);
42 }
43 else
44 _amsg_exit(_RT_THREAD); /* write message and die */
45 }
46
47 SetLastError(TL_LastError);
48
49
50