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

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 change the position of a gameobject in c# unity 
Csharp :: c# console writeline array 
Csharp :: unity agent walks in place at start 
Csharp :: remove backcolor c# 
Csharp :: unity how to end a game with esc 
Csharp :: c# winforms textbox readonly 
Csharp :: c# thread sleep 
Csharp :: remove session in dotnet core 
Csharp :: aabb collision with direction 
Csharp :: iactionresult 
Csharp :: car controller script unity 
Csharp :: binding command to event wpf 
Csharp :: regex replace all special characters 
Csharp :: checkbox value unchecked after return view model 
Csharp :: c# datetime get number of week 
Csharp :: cinemachine namespace not working 
Csharp :: how to convert iformfile to byte array c# 
Csharp :: unity get child 
Csharp :: unity destroy all children 
Csharp :: c sharp split string 
Csharp :: alphabet string[] c# 
Csharp :: c# choose first n elements from list 
Csharp :: get normal from 3 points 
Csharp :: unity dictionary check if key exists 
Csharp :: wpf restart application c# 
Csharp :: discord bot status code c# 
Csharp :: if char is upper csharp 
Csharp :: how to print c# 
Csharp :: convert string to date in c# 
Csharp :: convert generic to type c# 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =