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 497897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:49:32+00:00 2026-05-13T05:49:32+00:00

I need to create a list of numbers from a range (for example from

  • 0

I need to create a list of numbers from a range (for example from x to y) in a random order so that every order has an equal chance.

I need this for a music player I write in C#, to create play lists in a random order.

Any ideas?

Thanks.

EDIT: I’m not interested in changing the original list, just pick up random indexes from a range in a random order so that every order has an equal chance.

Here’s what I’ve wrriten so far:

    public static IEnumerable<int> RandomIndexes(int count)
    {
        if (count > 0)
        {
            int[] indexes = new int[count];
            int indexesCountMinus1 = count - 1;

            for (int i = 0; i < count; i++)
            {
                indexes[i] = i;
            }

            Random random = new Random();

            while (indexesCountMinus1 > 0)
            {
                int currIndex = random.Next(0, indexesCountMinus1 + 1);
                yield return indexes[currIndex];

                indexes[currIndex] = indexes[indexesCountMinus1];
                indexesCountMinus1--;
            }

            yield return indexes[0];
        }
    }

It’s working, but the only problem of this is that I need to allocate an array in the memory in the size of count. I’m looking for something that dose not require memory allocation.

Thanks.

  • 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-13T05:49:32+00:00Added an answer on May 13, 2026 at 5:49 am

    This can actually be tricky if you’re not careful (i.e., using a naïve shuffling algorithm). Take a look at the Fisher-Yates/Knuth shuffle algorithm for proper distribution of values.

    Once you have the shuffling algorithm, the rest should be easy.

    Here’s more detail from Jeff Atwood.

    Lastly, here’s Jon Skeet’s implementation and description.

    EDIT

    I don’t believe that there’s a solution that satisfies your two conflicting requirements (first, to be random with no repeats and second to not allocate any additional memory). I believe you may be prematurely optimizing your solution as the memory implications should be negligible, unless you’re embedded. Or, perhaps I’m just not smart enough to come up with an answer.

    With that, here’s code that will create an array of evenly distributed random indexes using the Knuth-Fisher-Yates algorithm (with a slight modification). You can cache the resulting array, or perform any number of optimizations depending on the rest of your implementation.

      private static int[] BuildShuffledIndexArray( int size ) {
    
         int[] array = new int[size];
         Random rand = new Random();
         for ( int currentIndex = array.Length - 1; currentIndex > 0; currentIndex-- ) {
            int nextIndex = rand.Next( currentIndex + 1 );
            Swap( array, currentIndex, nextIndex );
         }
         return array;
      }
    
      private static void Swap( IList<int> array, int firstIndex, int secondIndex ) {
    
         if ( array[firstIndex] == 0 ) {
            array[firstIndex] = firstIndex;
         }
         if ( array[secondIndex] == 0 ) {
            array[secondIndex] = secondIndex;
         }
         int temp = array[secondIndex];
         array[secondIndex] = array[firstIndex];
         array[firstIndex] = temp;
      }
    

    NOTE: You can use ushort instead of int to half the size in memory as long as you don’t have more than 65,535 items in your playlist. You could always programmatically switch to int if the size exceeds ushort.MaxValue. If I, personally, added more than 65K items to a playlist, I wouldn’t be shocked by increased memory utilization.

    Remember, too, that this is a managed language. The VM will always reserve more memory than you are using to limit the number of times it needs to ask the OS for more RAM and to limit fragmentation.

    EDIT

    Okay, last try: we can look to tweak the performance/memory trade off: You could create your list of integers, then write it to disk. Then just keep a pointer to the offset in the file. Then every time you need a new number, you just have disk I/O to deal with. Perhaps you can find some balance here, and just read N-sized blocks of data into memory where N is some number you’re comfortable with.

    Seems like a lot of work for a shuffle algorithm, but if you’re dead-set on conserving memory, then at least it’s an option.

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

Sidebar

Related Questions

I need to create a non sequential list of numbers that fit within a
I`v come across a need where I want to create multiple list items from
I need to create an XML schema that looks something like this: <xs:element name=wrapperElement>
I need to create user control for SharePoint to list all users from Active
I need a way to create an invoice from multiple orders every month? In
You often need View in View. For example, a client that has many phone
I wrote this code to create a list from en number of arguments given
i need create an email list sending to many emails. what is best solution
How can I create a list in C++? I need it to create a
I need to create tasks in a SharePoint Task List with c#. Can I

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.