Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# list shuffle

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

Comment

c# shuffle array

# cCopyusing System;
using System.Linq;
using System.Security.Cryptography;

namespace randomize_array
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = { 1, 2, 3, 4, 5 };
            Random random = new Random();
            arr = arr.OrderBy(x => random.Next()).ToArray();
            foreach (var i in arr)
            {
                Console.WriteLine(i);
            }
        }
    }
}

3
4
5
1
2
Comment

c# shuffle list

for (int i = 0; i < fruitsList.Count; i++)
{
  Fruit fruitCurrentIndex = fruitsList[i];
  int randomIndex = Random.Range(i, fruitsList.Count);
  fruitsList[i] = fruitsList[randomIndex];
  fruitsList[randomIndex] = fruitCurrentIndex;
}
Comment

c# list shuffle


private static Random rng = new Random();  

public static void Shuffle<T>(this IList<T> list)  
{  
    int n = list.Count;  
    while (n > 1) {  
        n--;  
        int k = rng.Next(n + 1);  
        T value = list[k];  
        list[k] = list[n];  
        list[n] = value;  
    }  
}

Comment

Shuffle Array C#

using System;
using System.Linq;
using System.Security.Cryptography;

{
    class Program
    {
        static void Main(string[] args)
        {
        	//Array of int
            int[] arr = { 1, 2, 3, 4, 5 };
            //Assigned the function 'Random()' to 'random'
            Random random = new Random();
            //generated a random index with 'random.Next()' and placed every int in a 
            //random index with 'OrderBy()'
            //converted the data in an array with 'ToArray()'
            arr = arr.OrderBy(x => random.Next()).ToArray();
            
            //Stamp the results
            foreach (var i in arr)
            {
                Console.WriteLine(i);
            }
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: Unity disable turn off component 
Csharp :: c# datetime remove time 
Csharp :: VLC .net 
Csharp :: unity3d remove parent 
Csharp :: all month in array 
Csharp :: unity spherecast 
Csharp :: unity create a child object 
Csharp :: hashing a file in C# 
Csharp :: c# wpf change label text color 
Csharp :: unity set dropdown value 
Csharp :: how to move a gameobject to another object 
Csharp :: How to create connection string dynamically in C# 
Csharp :: unity topdown 
Csharp :: console.writeline c# 
Csharp :: onkeypressed unity 
Csharp :: Cursor Lock and Visible in Unity 
Csharp :: copy 2d arrays C# 
Csharp :: how to store array in c# 
Csharp :: c# initialize empty array 
Csharp :: string reverse c# 
Csharp :: unity random number 
Csharp :: function in c# to do addition 
Csharp :: move files from one folder to another using c# 
Csharp :: list to list<selectlistitem c# 
Csharp :: char contains c# 
Csharp :: linq where 
Csharp :: linear search c# 
Csharp :: unity string lowercase 
Csharp :: merge xml files into one c# 
Csharp :: unity c# audio source 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =