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

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

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 :: how to make panel scrollable c# 
Csharp :: how to add a queue unity 
Csharp :: c# environment variables 
Csharp :: c# bytes to image 
Csharp :: c# initialize empty array 
Csharp :: unit test throw exception c# xunit 
Csharp :: c# get datatable column names to list 
Csharp :: c# list string return concatenate 
Csharp :: c# string remove 
Csharp :: c# find largest number in list 
Csharp :: c# String.Concat() 
Csharp :: how to install jdk on linux manjaro 
Csharp :: c# string contains any of list 
Csharp :: c# create list with range 
Csharp :: c# for loop 
Csharp :: ngrok for https 
Csharp :: how to generate random number in unity 
Csharp :: process.start web 
Csharp :: how to make colliders collide with some things but not other in unity 
Csharp :: unity action example 
Csharp :: {"The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."} 
Csharp :: c# iterate enum 
Csharp :: unity color by rgb 
Csharp :: relative path c# 
Csharp :: xmldocument to c# object 
Csharp :: c# multi assignment 
Csharp :: c# get foreground window 
Csharp :: how to select time and date in datetimepicker in c# 
Csharp :: unity setparent 
Csharp :: C# return and set multiple values from method 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =