全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1644
推到 Plurk!
推到 Facebook!

随机数产生函数

尚未結案
小丫123
初階會員


發表:85
回覆:62
積分:29
註冊:2004-08-31

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-10-16 10:59:32 IP:218.64.xxx.xxx 未訂閱
请教各位: 1.C语言中“返回0-1的双精度随机数”: double drnd() {return((double)rand()/(double)bigrnd)} 其中bigrnd为宏定义的数 那在delphi中是不是 function drnd() { result=rand()div bigrnd; return(result); } 2.在delphi中怎样申请1维双精度实数数组?
wameng
版主


發表:31
回覆:1336
積分:1188
註冊:2004-09-16

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-10-16 11:53:32 IP:61.222.xxx.xxx 未訂閱
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

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-10-16 12:17:19 IP:211.76.xxx.xxx 未訂閱
小丫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

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-10-16 20:37:49 IP:218.64.xxx.xxx 未訂閱
多谢两位,第一个问题解决了,能不能再帮我解决第二个问题呢?谢谢
小丫123
初階會員


發表:85
回覆:62
積分:29
註冊:2004-08-31

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-10-16 21:34:37 IP:218.64.xxx.xxx 未訂閱
在C中申请1维双精度实数数组为: double *alloc_1d_dbl(int n) { double *new1; new1=(double*)malloc((unsigned)(n*sizeof(double))); if (new1==null) {printf("Couldn't allocate array of doubles\n"); return(null); } return(new1); } 在delphi中怎么编呢?
richtop
資深會員


發表:122
回覆:646
積分:468
註冊:2003-06-10

發送簡訊給我
#6 引用回覆 回覆 發表時間:2004-10-17 18:50:50 IP:211.76.xxx.xxx 未訂閱
小丫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

發送簡訊給我
#7 引用回覆 回覆 發表時間:2004-10-17 19:55:41 IP:218.64.xxx.xxx 未訂閱
richtop您好,你的回答又帮我解决了这个问题,可是看了之后我又不禁想知道如果这个动态数组是二维的,那我又该怎么编呢?
小丫123
初階會員


發表:85
回覆:62
積分:29
註冊:2004-08-31

發送簡訊給我
#8 引用回覆 回覆 發表時間:2004-10-17 20:17:13 IP:218.64.xxx.xxx 未訂閱
在delphi线上说明中我找到了答案,多谢richtop的细心指导
系統時間:2024-06-30 13:38:14
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!