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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:09:07+00:00 2026-05-25T00:09:07+00:00

I am trying to use the Fisher-Yates algorithm to shuffle a stack of elements.

  • 0

I am trying to use the Fisher-Yates algorithm to shuffle a stack of elements. I’m having trouble passing in the stack by reference. The code below gives the error “Iterators cannot have ref or out parameters”. How do I get the algorithm to act on the actual stack that gets passed in?

Thanks.

Code is below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
public static class Doshuffle
{
    public static IEnumerable<T> Shuffle<T>(ref Stack<T> source)
    {

        Random rng = new Random();
        T[] elements = source.ToArray();
        source.Clear();
        // Note i > 0 to avoid final pointless iteration
        for (int i = elements.Length - 1; i > 0; i--)
        {
            // Swap element "i" with a random earlier element it (or itself)
            int swapIndex = rng.Next(i + 1);
            T tmp = elements[i];
            elements[i] = elements[swapIndex];
            elements[swapIndex] = tmp;
        }
        // Lazily yield (avoiding aliasing issues etc)
        foreach (T element in elements)
        {
            source.Push(element);
            yield return element;
        }
    }
}

}

  • 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-25T00:09:07+00:00Added an answer on May 25, 2026 at 12:09 am

    How do I get the algorithm to act on the actual stack that gets passed
    in?

    You do not need a ref parameter here since Stack<T> is a reference type and you are not trying to re-assign the reference itself.

    References are by default passed by value but that value (the reference) points to the same object on the heap, in other words you have two references pointing to the same object which is fine – all operations will be executed on the original Stack<T> object.

    Edit:

    In light of your comment I suggest you redesign to not modify the original Stack<T> which is troublesome to begin with:

    public static IEnumerable<T> Shuffle<T>(Stack<T> source)
    {
        Random rng = new Random();
        T[] elements = source.ToArray();
        // Note i > 0 to avoid final pointless iteration
        for (int i = elements.Length - 1; i > 0; i--)
        {
            // Swap element "i" with a random earlier element it (or itself)
            int swapIndex = rng.Next(i + 1);
            T tmp = elements[i];
            elements[i] = elements[swapIndex];
            elements[swapIndex] = tmp;
        }
        // Lazily yield (avoiding aliasing issues etc)
        foreach (T element in elements)
        {
            yield return element;
        }
    }
    

    Now you can just use it like this:

    foreach (var item in Doshuffle.Shuffle(gameDeck)) 
    { 
       System.Console.WriteLine(item.cardName); 
    }
    

    Also be careful with your use of Random – you might want to pass it in. At this point you can use Jon Skeet’s Shuffle implementation instead of your own – it’s better to reuse than reinvent.

    Final Edit:

    It looks like you just want to shuffle your Stack<T> in place -use an extension method instead:

    public static void Shuffle<T>(this Stack<T> source)
    {
        Random rng = new Random();
        T[] elements = source.ToArray();
        source.Clear();
        // Note i > 0 to avoid final pointless iteration
        for (int i = elements.Length - 1; i > 0; i--)
        {
            // Swap element "i" with a random earlier element it (or itself)
            int swapIndex = rng.Next(i + 1);
            T tmp = elements[i];
            elements[i] = elements[swapIndex];
            elements[swapIndex] = tmp;
        }
        foreach (T element in elements)
        {
            source.Push(element);
        }
    }
    

    Now you can just do:

    gameStack.Shuffle();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying use the code shown below to plot in such a way
Trying to use an excpetion class which could provide location reference for XML parsing,
Trying to use this code to connect the AD PrincipalContext context = new PrincipalContext(ContextType.Domain,
trying to use the preferred on method call but the code is not working
I am trying use a javascript function while passing php variables in it. For
I am trying use a javascript function while passing php variables in it. For
I'm trying to use the fisher.test function, which is called from a perl script
I'm trying use Google iosched as a reference project to learn the best practices
Im trying use a Java annotation in a Groovy class but have trouble to
I was trying use a set of filter functions to run the appropriate routine,

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.