2014-09-24 36 views
0

我有這個程序在FormActivate上運行,它使用隨機化程序,但是給出了重複的結果。我使用變量OutText顯示結果,並返回非常重複/可預測的數字(例如,444555666!444555666)。Randomize function repetative/predicable

procedure TMainWin.FormActivate(Sender: TObject); 
    var 
    i, i2, TileValue : integer; 
    OutText : String; 
begin 

    MainWin.Width := SCALE * WIDTH; 
    MainWin.Height := SCALE * HEIGHT; 

    OutText := ''; 

    for i := 1 to 5 do begin 

    Randomize; 
    TileValue := Random(20) + 1; 
    arTilesFinal[i, TileValue] := 1; 

    for i2 := 1 to 2 do begin 
     While arTilesFinal[i, TileValue] > 0 do begin 
     Randomize; 
     TileValue := Random(20) + 1; 
     end; 
     arTilesFinal[i, TileValue] := 2; 
    end; 

    for i2 := 1 to 2 do begin; 
     While arTilesFinal[i, TileValue] > 0 do begin 
     Randomize; 
     TileValue := Random(20) + 1; 
     end; 
     arTilesFinal[i, TileValue] := 3; 
    end; 

    for i2 := 1 to 5 do begin; 
     While arTilesFinal[i, TileValue] > 0 do begin 
     Randomize; 
     TileValue := Random(20) + 1; 
     end; 
     arTilesFinal[i, TileValue] := 4; 
    end; 

    for i2 := 1 to 5 do begin; 
     While arTilesFinal[i, TileValue] > 0 do begin 
     Randomize; 
     TileValue := Random(20) + 1; 
     end; 
     arTilesFinal[i, TileValue] := 5; 
    end; 

    for i2 := 1 to 5 do begin; 
     While arTilesFinal[i, TileValue] > 0 do begin 
     Randomize; 
     TileValue := Random(20) + 1; 
     end; 
     arTilesFinal[i, TileValue] := 6; 
    end; 
    end; 

    for i := 1 to 5 do begin 
    for i2 := 1 to 20 do begin 
     OutText := OutText + IntToStr(arTilesFinal[i,i2]) + ','; 
    end; 
    OutText := OutText + ' ! '; 
    end; 

    ShowMessage(OutText); 
    //AddImages; 
end; 

aTiles和aTilesFinal定義如下

type 
    aTiles = array[1..20] of integer; 
    aTilesFinal = array[1..5] of aTiles; 

var 
    arTilesFinal : aTilesFinal; 

回答

2

您不得以隨機循環每次調用「隨機」。 隨機調用一次,然後執行所有隨機函數調用。

+0

我除去了第一個以外的所有隨機呼叫,它現在給出了更好的結果。謝謝 – AlphaMc111 2014-09-24 13:49:39

+0

@ AlphaMc111:將剩餘的唯一調用移動到'Randomize'到你的表單的'OnCreate'處理程序。應用程序運行期間應該只調用一次;將它放在OnActivate中會在你的表單獲得焦點時調用它,這可能導致多次調用。 – 2014-09-24 16:46:35

+1

'randomize'的最佳位置在'.dpr'文件中 – 2014-09-24 20:20:37