随机数产生函数 |
尚未結案
|
小丫123
初階會員 發表:85 回覆:62 積分:29 註冊:2004-08-31 發送簡訊給我 |
|
wameng
版主 發表:31 回覆:1336 積分:1188 註冊:2004-09-16 發送簡訊給我 |
function Random [ ( Range: Integer) ]; Random returns a random number within the range 0 <= X < Range. If Range is not specified, the result is a real-type random number within the range. 亂數函數 Random(範圍) 如:Random(100) 所產生的亂數範圍為 0~99 在 C 裡面 rand() 若不帶入參數,則亂數範圍為 0~32767
就等於Delphi的 Random(32768); Function Drnd : Double;
begin
Result := Random(32768) / bigrnd;
end;
我不知道 bigrnd 是什麼咚咚?
PS. Div 是求商數。(會變成整數)
|
richtop
資深會員 發表:122 回覆:646 積分:468 註冊:2003-06-10 發送簡訊給我 |
小丫123 您好: 經您提問才發現Delphi好像真的沒有浮點數形式的隨機數。
如果按照您的說明可以修改出Delphi的uniform (0~1)隨機數,如下述程式。
當然啦!如果要每次執行時產生的隨機數序列都不一樣,則可加上randomize;
反之,如果要每次產生的隨機數序列都一樣,也可透過設定的RandSeed值來達成。
function uniformRand() : double; begin // High(integer) = MaxInt; result := Random(High(integer)) / MaxInt; end; procedure TForm1.Button1Click(Sender: TObject); var k : integer; msg : string; begin // RandSeed := 3; // for example, if repeated sequence needed. randomize; for k:=1 to 10 do begin msg := msg FloatToStr(uniformRand()) #13; end; ShowMessage( msg ); end;RichTop 敬上 =====***** 把數學當工具,可以解決問題;將數學變能力,能夠發現並解決問題! =====##### |
小丫123
初階會員 發表:85 回覆:62 積分:29 註冊:2004-08-31 發送簡訊給我 |
|
小丫123
初階會員 發表:85 回覆:62 積分:29 註冊:2004-08-31 發送簡訊給我 |
|
richtop
資深會員 發表:122 回覆:646 積分:468 註冊:2003-06-10 發送簡訊給我 |
小丫123 您好: 您的意思好像是要動態產生陣列。
底下方法可以完成,詳細的內容可參考 class="code">procedure TForm1.Button1Click(Sender: TObject);
var
myAry : array of double;
begin
SetLength(myAry, 20); // allocate the dynamic array
ShowMessage('(' IntToStr(Low(myAry)) ',' IntToStr(High(myAry)) ')'); SetLength(myAry, 10); // reallocate the dynamic array
ShowMessage('(' IntToStr(Low(myAry)) ',' IntToStr(High(myAry)) ')'); SetLength(myAry,0); // equivalent to release the dynamic array.
end; RichTop 敬上 =====*****
把數學當工具,可以解決問題;將數學變能力,能夠發現並解決問題!
=====#####
|
小丫123
初階會員 發表:85 回覆:62 積分:29 註冊:2004-08-31 發送簡訊給我 |
|
小丫123
初階會員 發表:85 回覆:62 積分:29 註冊:2004-08-31 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |