?? random.asm.htm
字號:
<font color=gray>001</font> <font color=green>;**************************************************</font><br><font color=gray>002</font> <font color=green>; 以下代碼由“老羅代碼著色器”0.2版進行著色</font><br><font color=gray>003</font> <font color=green>; 測試文件名:Random.asm</font><br><font color=gray>004</font> <font color=green>; 測試日期: 2002-12-29</font><br><font color=gray>005</font> <font color=green>;**************************************************</font><br><font color=gray>006</font> <br><font color=gray>007</font> <font color=green>;*********************************************************</font><br><font color=gray>008</font> <font color=green>;程序名稱:隨機數的產生原理與實現</font><br><font color=gray>009</font> <font color=green>;作者:羅聰</font><br><font color=gray>010</font> <font color=green>;日期:2002-11-21</font><br><font color=gray>011</font> <font color=green>;出處:http://www.LuoCong.com(老羅的繽紛天地)</font><br><font color=gray>012</font> <font color=green>;注意事項:如欲轉載,請保持本程序的完整,并注明:</font><br><font color=gray>013</font> <font color=green>;轉載自“老羅的繽紛天地”(http://www.LuoCong.com)</font><br><font color=gray>014</font> <font color=green>;*********************************************************</font><br><font color=gray>015</font> <br><font color=gray>016</font> <font color=red>.386</font><br><font color=gray>017</font> <font color=red>.model</font> <font color=red>flat</font>, <font color=red>stdcall</font><br><font color=gray>018</font> <font color=blue>option</font> <font color=red>casemap</font>:<font color=red>none</font><br><font color=gray>019</font> <br><font color=gray>020</font> <font color=blue>include</font> \masm32\<font color=blue>include</font>\windows.inc<br><font color=gray>021</font> <font color=blue>include</font> \masm32\<font color=blue>include</font>\kernel32.inc<br><font color=gray>022</font> <font color=blue>include</font> \masm32\<font color=blue>include</font>\user32.inc<br><font color=gray>023</font> <font color=blue>includelib</font> \masm32\<font color=red>lib</font>\kernel32.lib<br><font color=gray>024</font> <font color=blue>includelib</font> \masm32\<font color=red>lib</font>\user32.lib<br><font color=gray>025</font> <br><font color=gray>026</font> WndProc <font color=blue>proto</font> :<font color=red>DWORD</font>, :<font color=red>DWORD</font>, :<font color=red>DWORD</font>, :<font color=red>DWORD</font><br><font color=gray>027</font> iRand <font color=blue>proto</font> :<font color=red>DWORD</font>, :<font color=red>DWORD</font><br><font color=gray>028</font> <br><font color=gray>029</font> <font color=blue>.const</font><br><font color=gray>030</font> IDC_BUTTON_GENERATE <font color=red>equ</font> 3000<br><font color=gray>031</font> IDC_EDIT_FIRST <font color=red>equ</font> 3001<br><font color=gray>032</font> IDC_EDIT_SECOND <font color=red>equ</font> 3002<br><font color=gray>033</font> <br><font color=gray>034</font> <font color=blue>.data</font><br><font color=gray>035</font> szDlgName <font color=red>db</font> <font color=orange>"lc_dialog"</font>, 0<br><font color=gray>036</font> szCaption <font color=red>db</font> <font color=orange>"Rand Number Generator by LC"</font>, 0<br><font color=gray>037</font> szText <font color=red>db</font> 255 dup(0)<br><font color=gray>038</font> szTemplate <font color=red>db</font> <font color=orange>"(%d ~ %d)隨機數:"</font>, 13, 10, 13, 10,\<br><font color=gray>039</font> <font color=orange>" %d"</font>, 13, 10, 13, 10,\<br><font color=gray>040</font> <font color=orange>"老羅的繽紛天地"</font>, 13, 10,\<br><font color=gray>041</font> <font color=orange>"http://www.LuoCong.com"</font>, 0<br><font color=gray>042</font> nFirst <font color=red>dd</font> 0<br><font color=gray>043</font> nSecond <font color=red>dd</font> 0<br><font color=gray>044</font> <br><font color=gray>045</font> <font color=blue>.code</font><br><font color=gray>046</font> main:<br><font color=gray>047</font> <font color=red>invoke</font> GetModuleHandle, NULL<br><font color=gray>048</font> <font color=red>invoke</font> DialogBoxParam, <font color=#808000>eax</font>, <font color=red>offset</font> szDlgName, 0, WndProc, 0<br><font color=gray>049</font> <font color=red>invoke</font> ExitProcess, <font color=#808000>eax</font><br><font color=gray>050</font> <br><font color=gray>051</font> WndProc <font color=red>proc</font> hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM<br><font color=gray>052</font> <font color=red>LOCAL</font> hEdit: HWND<br><font color=gray>053</font> <br><font color=gray>054</font> .if uMsg == WM_CLOSE<br><font color=gray>055</font> <font color=red>invoke</font> EndDialog, hWnd, 0<br><font color=gray>056</font> <br><font color=gray>057</font> .elseif uMsg == WM_COMMAND<br><font color=gray>058</font> <font color=blue>mov</font> <font color=#808000>eax</font>, wParam<br><font color=gray>059</font> <font color=blue>mov</font> <font color=#808000>edx</font>, <font color=#808000>eax</font><br><font color=gray>060</font> <font color=blue>shr</font> <font color=#808000>edx</font>, 16<br><font color=gray>061</font> <font color=blue>movzx</font> <font color=#808000>eax</font>, <font color=#808000>ax</font><br><font color=gray>062</font> .if <font color=#808000>edx</font> == BN_CLICKED<br><font color=gray>063</font> .if <font color=#808000>eax</font> == IDCANCEL<br><font color=gray>064</font> <font color=red>invoke</font> EndDialog, hWnd, NULL<br><font color=gray>065</font> .elseif <font color=#808000>eax</font> == IDC_BUTTON_GENERATE || <font color=#808000>eax</font> == IDOK<br><font color=gray>066</font> <font color=green>;獲得上限:</font><br><font color=gray>067</font> <font color=red>invoke</font> GetDlgItemInt, hWnd, IDC_EDIT_FIRST, NULL, <font color=blue>TRUE</font><br><font color=gray>068</font> <font color=blue>mov</font> nFirst, <font color=#808000>eax</font><br><font color=gray>069</font> <font color=green>;獲得下限:</font><br><font color=gray>070</font> <font color=red>invoke</font> GetDlgItemInt, hWnd, IDC_EDIT_SECOND, NULL, <font color=blue>TRUE</font><br><font color=gray>071</font> <font color=blue>mov</font> nSecond, <font color=#808000>eax</font><br><font color=gray>072</font> <font color=green>;產生隨機數:</font><br><font color=gray>073</font> <font color=red>invoke</font> iRand, nFirst, nSecond<br><font color=gray>074</font> <font color=green>;輸出:</font><br><font color=gray>075</font> <font color=red>invoke</font> wsprintf, <font color=red>addr</font> szText, <font color=red>addr</font> szTemplate, nFirst, nSecond, <font color=#808000>eax</font><br><font color=gray>076</font> <font color=red>invoke</font> MessageBox, hWnd, <font color=red>addr</font> szText, <font color=red>addr</font> szCaption, MB_OK <font color=blue>or</font> MB_ICONINFORMATION<br><font color=gray>077</font> .endif<br><font color=gray>078</font> .endif<br><font color=gray>079</font> .else<br><font color=gray>080</font> <font color=blue>mov</font> <font color=#808000>eax</font>, <font color=blue>FALSE</font><br><font color=gray>081</font> <font color=blue>ret</font><br><font color=gray>082</font> .endif<br><font color=gray>083</font> <font color=blue>mov</font> <font color=#808000>eax</font>, <font color=blue>TRUE</font><br><font color=gray>084</font> <font color=blue>ret</font><br><font color=gray>085</font> WndProc <font color=red>endp</font><br><font color=gray>086</font> <br><font color=gray>087</font> <font color=green>;**********************************************************************</font><br><font color=gray>088</font> <font color=green>; 函數功能:產生范圍從 first 到 second 的隨機數</font><br><font color=gray>089</font> <font color=green>; 傳入參數:</font><br><font color=gray>090</font> <font color=green>; first = 下限</font><br><font color=gray>091</font> <font color=green>; second = 上限</font><br><font color=gray>092</font> <font color=green>; 返回參數:</font><br><font color=gray>093</font> <font color=green>; eax = Rand_Number</font><br><font color=gray>094</font> <font color=green>; 所用公式:</font><br><font color=gray>095</font> <font color=green>; Rand_Number = (Rand_Seed * X + Y) mod Z</font><br><font color=gray>096</font> <font color=green>; 補充說明:</font><br><font color=gray>097</font> <font color=green>; (1)本例中用 GetTickCount 來取得隨機數種子,</font><br><font color=gray>098</font> <font color=green>; 在實際應用中,可用別的方法代替。</font><br><font color=gray>099</font> <font color=green>; (2)要產生隨機數,X和Y其中之一必須是素數,</font><br><font color=gray>100</font> <font color=green>; 所以 X = 23, Y = 7(可用別的素數代替)</font><br><font color=gray>101</font> <font color=green>;**********************************************************************</font><br><font color=gray>102</font> iRand <font color=red>proc</font> <font color=red>uses</font> <font color=#808000>ecx</font> <font color=#808000>edx</font> first:<font color=red>DWORD</font>, second:<font color=red>DWORD</font><br><font color=gray>103</font> <font color=red>invoke</font> GetTickCount <font color=green>; 取得隨機數種子,當然,可用別的方法代替</font><br><font color=gray>104</font> <font color=blue>mov</font> <font color=#808000>ecx</font>, 23 <font color=green>; X = ecx = 23</font><br><font color=gray>105</font> <font color=blue>mul</font> <font color=#808000>ecx</font> <font color=green>; eax = eax * X</font><br><font color=gray>106</font> <font color=blue>add</font> <font color=#808000>eax</font>, 7 <font color=green>; eax = eax + Y (Y = 7)</font><br><font color=gray>107</font> <font color=blue>mov</font> <font color=#808000>ecx</font>, second <font color=green>; ecx = 上限</font><br><font color=gray>108</font> <font color=blue>sub</font> <font color=#808000>ecx</font>, first <font color=green>; ecx = 上限 - 下限</font><br><font color=gray>109</font> <font color=blue>inc</font> <font color=#808000>ecx</font> <font color=green>; Z = ecx + 1 (得到了范圍)</font><br><font color=gray>110</font> <font color=blue>xor</font> <font color=#808000>edx</font>, <font color=#808000>edx</font> <font color=green>; edx = 0</font><br><font color=gray>111</font> <font color=blue>div</font> <font color=#808000>ecx</font> <font color=green>; eax = eax mod Z (余數在edx里面)</font><br><font color=gray>112</font> <font color=blue>add</font> <font color=#808000>edx</font>, first <font color=green>; 修正產生的隨機數的范圍</font><br><font color=gray>113</font> <font color=blue>mov</font> <font color=#808000>eax</font>, <font color=#808000>edx</font> <font color=green>; eax = Rand_Number</font><br><font color=gray>114</font> <font color=blue>ret</font><br><font color=gray>115</font> iRand <font color=red>endp</font><br><font color=gray>116</font> <br><font color=gray>117</font> <font color=blue>end</font> main<br><font color=gray>118</font> <font color=green>;******************** over ********************</font><br><font color=gray>119</font> <font color=green>;by LC</font>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -