Чем можно рандомно перемешать строки в файле txt?

Discussion in 'Болталка' started by acult, 9 Jun 2011.

  1. acult

    acult Member

    Joined:
    9 Jan 2010
    Messages:
    0
    Likes Received:
    65
    Reputations:
    5
    .
     
    #1 acult, 9 Jun 2011
    Last edited: 5 Oct 2016
  2. CodeSender:)

    CodeSender:) Elder - Старейшина

    Joined:
    29 Jul 2010
    Messages:
    245
    Likes Received:
    115
    Reputations:
    23
    KeyWordKeeper
     
  3. Skofield

    Skofield Elder - Старейшина

    Joined:
    27 Aug 2008
    Messages:
    960
    Likes Received:
    392
    Reputations:
    58
    PHP:
    <?php
    $f_name 
    "file.txt";
    $arr file($f_name); 
    shuffle($arr);
    $source implode(''$arr); 
    file_put_contents($f_name$source); 
    ?>
     
  4. Ins3t

    Ins3t Харьковчанин

    Joined:
    18 Jul 2009
    Messages:
    939
    Likes Received:
    429
    Reputations:
    139
    std::random_shuffle() for C++
     
  5. CodeSender:)

    CodeSender:) Elder - Старейшина

    Joined:
    29 Jul 2010
    Messages:
    245
    Likes Received:
    115
    Reputations:
    23
    Ок, C#:
    Random random = new Random();
    int[] sourceArray = new int[] {0, 1, 2, 3, 4, 5, 6};
    int[] randomArray = sourceArray.OrderBy(i => random.Next()).ToArray();

    Похоже в этом треде будет перемешивание массива на всех ЯП...
     
  6. Skofield

    Skofield Elder - Старейшина

    Joined:
    27 Aug 2008
    Messages:
    960
    Likes Received:
    392
    Reputations:
    58
    Ну, тогда уж давайте остальные яп :), и получится супер рандомизация)
     
  7. CodeSender:)

    CodeSender:) Elder - Старейшина

    Joined:
    29 Jul 2010
    Messages:
    245
    Likes Received:
    115
    Reputations:
    23
    [Python]
    import random
    random.shuffle(array)

    [JS]
    Array.prototype.shuffle = function() {
    for (var i = this.length - 1; i > 0; i--) {
    var num = Math.floor(Math.random() * (i + 1));
    var d = this[num];
    this[num] = this;
    this = d;
    }
    return this;
    }

    [Pascal]
    type mas=array[1..100]of integer;

    function shuffle(a:mas,n:integer):mas;
    var b,c1,c2:integer;
    begin
    randomize;
    for i:=1 to n do //чем больше конечное значение цикла, тем более "перемешанней" будет массив
    begin
    c1:=random(n)+1;
    c2:=random(n)+1;
    b:=a[c1];
    a[c1]:=a[c2];
    a[c2]:=b;
    end;
    shuffle:=a;
    end;

    [delphi]
    procedure FillArray(var A:array of Integer);
    var I, S, R: Integer;
    begin
    randomize;
    for i:=0 to High(A) do A:=i;
    for i:=High(A) downto 0 do begin
    R:=Random(I);
    S:=A[R];A[R]:=A;A:=S;
    end;
    end;


    procedure TForm1.Button1Click(Sender: TObject);
    var mas,t:array[0..1000] of Integer;
    i,a,b:integer;
    begin
    memo1.Clear;
    for i:=0 to 1000 do mas:=1000+i;
    for i:=0 to 1000 do mas:=i;
    FillArray(t);//массив перемешенных индексов
    for i:=0 to 1000 do begin //меняет i-ое на t
    a:=mas;
    b:=mas[t];
    mas:=b;
    mas[t]:=a;
    end;
    for i:=0 to 1000 do memo1.Lines.add(inttostr(mas));
    end;
     
  8. Skofield

    Skofield Elder - Старейшина

    Joined:
    27 Aug 2008
    Messages:
    960
    Likes Received:
    392
    Reputations:
    58
    :D

    if (strlen($post) >= 4) {
    echo "Ok, you may post it ;)";
    }
     
  9. xBot

    xBot New Member

    Joined:
    13 Aug 2010
    Messages:
    3
    Likes Received:
    0
    Reputations:
    0
    проще всего LINQ-ом на C#, как выше написал товарищ
     
  10. AeroDEN

    AeroDEN Banned

    Joined:
    1 Aug 2009
    Messages:
    0
    Likes Received:
    0
    Reputations:
    0
    а отдельная программка существует без всяких скриптов?
     
  11. Skofield

    Skofield Elder - Старейшина

    Joined:
    27 Aug 2008
    Messages:
    960
    Likes Received:
    392
    Reputations:
    58
    sekator
     
  12. AeroDEN

    AeroDEN Banned

    Joined:
    1 Aug 2009
    Messages:
    0
    Likes Received:
    0
    Reputations:
    0
    sekator v.1.03 - отличная вещь! спасибо!