Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,971 questions

51,913 answers

573 users

How to shuffle (random) a list in C#

3 Answers

0 votes
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            var rnd = new Random();
            var r = list.OrderBy(item => rnd.Next());

            Console.WriteLine(String.Join(" ", r));
        }
    }
}


/*
run:

2 4 1 6 8 7 9 3 5

*/

 



answered Jul 31, 2018 by avibootz
0 votes
using System;
using System.Collections.Generic;

namespace ConsoleApplication_C_Sharp
{
    public static class SF
    {
        public static void Shuffle<T>(this IList<T> list)
        {
            int count = list.Count;
            Random rnd = new Random();
            while (count > 1) {
                int k = (rnd.Next(0, count) % count);
                count--;
                T value = list[k];
                list[k] = list[count];
                list[count] = value;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            SF.Shuffle(list);

            Console.WriteLine(String.Join(" ", list));
        }
       
    }
    
}


/*
run:

8 7 3 1 4 6 5 2 9

*/

 



answered Jul 31, 2018 by avibootz
0 votes
using System;
using System.Collections.Generic;

namespace ConsoleApplication_C_Sharp
{
    public static class SFL
    {
        public static void Shuffle<T>(this IList<T> list)
        {
            int count = list.Count;
            Random rnd = new Random();
            while (count > 1) {
                count--;
                int n = rnd.Next(count + 1);
                T value = list[n];
                list[n] = list[count];
                list[count] = value;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            SFL.Shuffle(list);

            Console.WriteLine(String.Join(" ", list));
        }
    }
}


/*
run:

9 1 6 5 7 2 8 4 3

*/

 



answered Jul 31, 2018 by avibootz

Related questions

1 answer 144 views
2 answers 176 views
176 views asked Aug 25, 2018 by avibootz
1 answer 162 views
162 views asked Oct 7, 2019 by avibootz
1 answer 144 views
144 views asked Oct 30, 2021 by avibootz
2 answers 445 views
1 answer 143 views
...