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

  • SEARCH
  • Home
  • 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 7592993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:03:11+00:00 2026-05-30T21:03:11+00:00

I need to shuffle part of an ArrayBuffer, preferably in-place so no copies are

  • 0

I need to shuffle part of an ArrayBuffer, preferably in-place so no copies are required. For example, if an ArrayBuffer has 10 elements, and I want to shuffle elements 3-7:

// Unshuffled ArrayBuffer of ints numbered 0-9
0, 1, 2, 3, 4, 5, 6, 7, 8, 9

// Region I want to shuffle is between the pipe symbols (3-7)
0, 1, 2 | 3, 4, 5, 6, 7 | 8, 9

// Example of how it might look after shuffling
0, 1, 2 | 6, 3, 5, 7, 4 | 8, 9

// Leaving us with a partially shuffled ArrayBuffer
0, 1, 2, 6, 3, 5, 7, 4, 8, 9

I’ve written something like shown below, but it requires copies and iterating over loops a couple of times. It seems like there should be a more efficient way of doing this.

def shufflePart(startIndex: Int, endIndex: Int) {

  val part: ArrayBuffer[Int] = ArrayBuffer[Int]()

  for (i <- startIndex to endIndex ) {
    part += this.children(i)
  }

  // Shuffle the part of the array we copied
  val shuffled = this.random.shuffle(part)
  var count: Int = 0

  // Overwrite the part of the array we chose with the shuffled version of it
  for (i <- startIndex to endIndex ) {
    this.children(i) = shuffled(count)
    count += 1
  }
}

I could find nothing about partially shuffling an ArrayBuffer with Google. I assume I will have to write my own method, but in doing so I would like to prevent copies.

  • 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-05-30T21:03:12+00:00Added an answer on May 30, 2026 at 9:03 pm

    I’m not entirely sure why it must be in place – you might want to think that over. But, assuming it’s the right thing to do, this could do it:

    import scala.collection.mutable.ArrayBuffer
    
    implicit def array2Shufflable[T](a: ArrayBuffer[T]) = new {
      def shufflePart(start: Int, end: Int) = {
        val seq = (start.max(0) until end.min(a.size - 1)).toSeq
        seq.zip(scala.util.Random.shuffle(seq)) foreach { t =>
          val x = a(t._1)
          a.update(t._1, a(t._2))
          a(t._2) = x
        }
        a
      }
    }
    

    You can use it like:

    val a = ArrayBuffer(1,2,3,4,5,6,7,8,9)
    println(a)
    println(a.shufflePart(2, 7))  
    

    edit: If you’re willing to pay the extra cost of an intermediate sequence, this is more reasonable, algorithmically speaking:

      def shufflePart(start: Int, end: Int) = {
        val seq = (start.max(0) until end.min(a.size - 1)).toSeq
        seq.zip(scala.util.Random.shuffle(seq) map { i =>
          a(i)
        }) foreach { t =>
          a.update(t._1, t._2)
        }
        a
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Part of my program requires me to be able to randomly shuffle list elements.
For a FFT function I need to permutate or shuffle the elements within an
I need a function that randomizes an array similar to what shuffle does, with
I have an unordered list. When i click a shuffle button, i need: the
I'm building a socket application that need to shuffle a lot of small/medium sized
I need to randomly shuffle the following Array: int[] solutionArray = {1, 2, 3,
As a part of my project I need to create non repeating 2 or
I need a shuffle albums algorithm for my audio player like in foobar2k. So
I need to shuffle an array based on a seed number so I can
Need to insert selected text on the page into textarea. There must be some

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.