2021년 8월 25일 수요일

Random number assignment without duplicates

Random number assignment without duplicates

type

  TDyArray = array of Integer;

//-----------------------------------------------------------

function Out_Random( cno : integer ) : TDyArray;

var

  r1, r2, itemp, i : integer;

  dataArr : TDyArray;

begin

  SetLength( dataArr, cno );

  for i := 0 to cno-1 do   // 동적 배열에 데이터를 할당

    dataArr[i] := i + 1;

  for I := 1 to cno div 2  do  // 전체갯수 반번 만큼 랜던 교환

  begin

    r1 := Random( cno );

    r2 := Random( cno );

    itemp := dataArr[ r1 ];

    dataArr[ r1 ] := dataArr[ r2 ];

    dataArr[ r2 ] := itemp;

  end;

  result := dataArr;

end;

// for Test ***************************************************************************

procedure TMForm.Button2Click(Sender: TObject);

var

  aaa : TDyArray;

  i  : integer;

begin

  aaa := Out_Random( 10 );


  Memo1.Lines.Clear;

  for i := 0 to High( aaa ) do

    Memo1.Lines.Add( aaa[ i ].ToString );

end;




댓글 없음:

댓글 쓰기