Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9118255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:06:09+00:00 2026-06-17T05:06:09+00:00

I want to randomize a number of string lists. The string lists all contain

  • 0

I want to randomize a number of string lists.

The string lists all contain the same number of items, and I wish to apply the same shuffle to each list. So if List1[0] is swapped with List1[7], then I want to swap List2[0] with List2[7], and so on for all the lists.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:06:10+00:00Added an answer on June 17, 2026 at 5:06 am

    I’m going to consider the case where you have two lists. I’ll leave it up to you to generalise the ideas to handle more than two lists. The key understanding is best gained using the most simple case where there are two lists.

    I would solve the problem like this:

    1. Generating a permutation of the integers 0, 1, … N-1. Use the Fisher–Yates shuffle to achieve this.
    2. Using that permutation to shuffle both lists.

    The key is to use the same permutation to shuffle both lists.

    type
      TIntegerArray = array of Integer;
    
    procedure Swap(var i1, i2: Integer); overload;
    var
      tmp: Integer;
    begin
      tmp := i1;
      i1 := i2;
      i2 := tmp;
    end;
    
    function GeneratePermutation(Count: Integer): TIntegerArray;
    //Fisher-Yates shuffle
    var
      i, j: Integer;
    begin
      SetLength(Result, Count);
      for i := 0 to Count-1 do
        Result[i] := i;
      for i := Count-1 downto 1 do begin
        j := Random(i+1);
        Swap(Result[i], Result[j]);
      end;
    end;
    
    procedure ApplyPermutation(List: TStringList; 
      const Permutation: TIntegerArray);
    var
      i: Integer;
      Temp: TStringList;
    begin
      Assert(List.Count=Length(Permutation));
      Temp := TStringList.Create;
      try
        Temp.Assign(List);
        for i := 0 to List.Count-1 do
          List[i] := Temp[Permutation[i]];
      finally
        Temp.Free;
      end;
    end;
    

    And then you can apply to your situation like this:

    Permutation := GeneratePermutation(List1.Count);
    Apply(List1, Permutation);
    Apply(List2, Permutation);
    

    This is an exceedingly general solution that can be extended to more than two lists, and can be applied to other data types. If you want a very short and simple dedicated routine then you can do it like this:

    procedure PermuteListsInTandem(List1, List2: TStringList);
    var
      i, j: Integer;
    begin
      Assert(List1.Count=List2.Count);
      for i := List1.Count-1 downto 1 do begin
        j := Random(i+1);
        List1.Exchange(i, j);
        List2.Exchange(i, j);
      end;
    end;
    

    I’m struggling to think of a good name for this procedure. Can anyone help me out by offering something better?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array @number = [1,2,3,4,5,6,7,8,9] Now, I want to randomize the array
I want to randomize a rubik's cube that is initialized as complete (all colors
I have List List<MyType> , my type contains Age and RandomID Now I want
want to know why String behaves like value type while using ==. String s1
i have a function that generates randomize numbers between 0 and 2,and i want
So I have a list of 4 strings in Python and I want to
I want to repeat a random number sequence generated by a legacy software using
I'm executing a query which I want to randomize query results from a drupal
What is the best way to randomize items in an array and also remove
I have a list with around 40 strings and want to assign every item

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.