Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# shuffle string array

public class Randomizer
{
    public static void Randomize<T>(T[] items)
    {
        Random rand = new Random();

        // For each spot in the array, pick
        // a random item to swap into that spot.
        for (int i = 0; i < items.Length - 1; i++)
        {
            int j = rand.Next(i, items.Length);
            T temp = items[i];
            items[i] = items[j];
            items[j] = temp;
        }
    }
}
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

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 :: c sharp array to list 
Csharp :: bubble sort in c# 
Csharp :: key press up unity 
Csharp :: prettier isnt working c# 
Csharp :: c# choose first n elements from list 
Csharp :: dart extending list 
Csharp :: c# thread sleep vs task delay 
Csharp :: C# convert iformfile to stream 
Csharp :: button not working unity 
Csharp :: C# Console multi language 
Csharp :: how to play animation with code in unity 
Csharp :: c# float to string with 2 decimals 
Csharp :: void to action c# 
Csharp :: mathf.clamp unity 
Csharp :: c# streamwriter 
Csharp :: c# loop through files in folder 
Csharp :: how to move towards an object unity 
Csharp :: Attribute [livewire] does not exist. 
Csharp :: get folders in directory c# 
Csharp :: How to get an array of months in c# 
Csharp :: c# number in range 
Csharp :: .net Core Return File like File Server 
Csharp :: unity keycode for f 
Csharp :: C# How to read users input and display it 
Csharp :: how to read values from appsettings.json in c# 
Csharp :: what is a protected int c# 
Csharp :: how to make unity build to not be full screen 
Csharp :: read embedded resource c# xml 
Csharp :: console reset color c# 
Csharp :: total months between two dates c# 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =