一個關於random的問題?如何從2至159中隨機輸出但不重複 |
答題得分者是:jow
|
017911
一般會員 發表:5 回覆:2 積分:1 註冊:2007-07-08 發送簡訊給我 |
如題
var a :integer; procedure TForm1.Button1Click(Sender: TObject); var b:boolean; c:integer; begin Randomize; a:=random(15) 2; c:=a; b:=false; if a<>c then b:=true; if b=true then showmessage(inttostr(a)); end; end. 這樣做不行 唔知點把輸出的數記下,然後把隨機的數比較,相同不輸出,到隨機的數不相同才輸出 |
jow
尊榮會員 發表:66 回覆:751 積分:1253 註冊:2002-03-13 發送簡訊給我 |
以下幾種方法, 提供你參考...
[code delphi] procedure TForm1.Button1Click(Sender: TObject); type//set operator 只適用數值介於0~255的狀況 TByteSet = set of 2..159; var B: Byte; BS: TByteSet; begin ListBox1.Clear; BS := [2..159]; repeat B := Random(160);//B := Random(MaxInt) mod 160; if B in BS then begin ListBox1.Items.Add(IntToStr(B));//output BS := BS - [B]; end; until BS = []; end; [/code] [code delphi] procedure TForm1.Button2Click(Sender: TObject); var B: Byte; I: Integer; L: TStringList; Key: string; begin ListBox1.Clear; L := TStringList.Create; try L.Sorted := True; repeat B := Random(160);//B := Random(MaxInt) mod 160; if B > 1 then begin Key := Format('%3.3d', [B]); if not L.Find(Key, I) then begin ListBox1.Items.Add(IntToStr(B));//output L.Add(Key); end; end; until L.Count = (159-2 1); finally FreeAndNil(L); end; end; [/code] [code delphi] procedure TForm1.Button3Click(Sender: TObject); var B: Byte; S, Key: string; Count: Integer; begin S := ''; ListBox1.Clear; Count := 159-2 1; repeat B := Random(160);//B := Random(MaxInt) mod 160; if B > 1 then begin Key := Format('%3.3d', [B]); if Pos(Key ',', S) = 0 then begin ListBox1.Items.Add(IntToStr(B));//output S := S Key ','; Dec(Count); end; end; until Count = 0; end; [/code] [code delphi] procedure TForm1.Button4Click(Sender: TObject); var B: Byte; Count: Integer; bools: array[2..159] of Boolean; begin ListBox1.Clear; FillChar(bools, SizeOf(bools), 0);//Initial Count := Length(bools); repeat B := Random(160);//B := Random(MaxInt) mod 160; if (B > 1) and not bools[B] then begin ListBox1.Items.Add(IntToStr(B));//output bools[B] := True; Dec(Count); end; until Count = 0; end; [/code]
編輯記錄
jow 重新編輯於 2007-11-13 00:14:05, 註解 無‧
|
shieh2700
高階會員 發表:0 回覆:127 積分:100 註冊:2002-06-13 發送簡訊給我 |
提供較為痴肥的方法.
[code delphi] procedure TForm1.Shuffle(list : TStrings); var i, r : integer; s : string; begin Randomize; for i := 0 to list.Count do begin r := RandomRange(i, list.Count); if (r = i) then Continue; s := list.Strings[i]; list.Strings[i] := list.Strings[r]; list.Strings[r] := s; end; end; procedure TForm1.Button1Click(Sender: TObject); var i : integer; begin memo1.Clear; for i := 2 to 159 do memo1.Lines.Add(IntToStr(i)); //這裡其實可以放任何東西, 麻將. 撲克... Shuffle(memo1.Lines); //打亂順序 end; [/code] |
jow
尊榮會員 發表:66 回覆:751 積分:1253 註冊:2002-03-13 發送簡訊給我 |
哈, 够好幾個人交作業了...
[code delphi] procedure TForm1.Button5Click(Sender: TObject); var S: string; begin ListBox1.Clear; with ListBox1, Items do repeat S := Format('%3.3d', [Random(MaxInt) mod 160]); if Pos(S, Text) = 0 then Add(S); until Count = 159-2 1; end; [/code] [code delphi] procedure TForm1.Button6Click(Sender: TObject); var S: string; begin with TStringList.Create do try repeat S := Format('%3.3d', [Random(MaxInt) mod 160]); if Pos(S, Text) = 0 then Add(S); until Count = 159-2 1; ListBox1.Items.Text := Text; finally Free; end; end; [/code] |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |