Shuffle Array on button

How to i create a shuffle aray in my scripts that shuffles the arrangement of these 5 cards

var selStrings : String[] =  ["Card 1", "Card 2", "Card 3", "Card 4", "Card 5"];
var deckRect : Rect = Rect (0,480,270,380);

private var cardsInHands : CardsInHands;


function Start() 
{
    cardsInHands = GameObject.FindGameObjectWithTag("Hands").GetComponent(CardsInHands);
}  
function Update ()    
{  if (selGridInt > 0)    { 
        switch (selGridInt) 
        {  
            case 1:
                break; 
            case 2:   
                break; 
            default:  
                break;   
        } 
    }    
}
function OnGUI () 
{
deckRect = GUI.Window (0, deckRect, DoMyDeckWindow, "Deck");
if (GUI.Button(Rect(boxPos.x, boxPos.y, 140, 40), "Draw"))
        {
            DrawCard();
        }
   }
function DrawCard()
{
    var tempCards : String[] = new String[selStrings.length-1];
    for (var i : int; i < selStrings.length; i++)
    {
        if (i == 0)
        {
            print(selStrings*);*

cardsInHands.AddCards(selStrings*);*
continue;
}
tempCards[i-1] = selStrings*;*
}
selStrings = new String[tempCards.length];
for (var j : int; j < tempCards.length; j++)
{
selStrings[j] = tempCards[j];
}
}
function DoMyDeckWindow (windowID : int) {
* scrollPosition = GUI.BeginScrollView (Rect (10,20,258,358), scrollPosition, Rect (0, 0, 230, 1200));*
* selGridInt = GUI.SelectionGrid (Rect (0, 0, 235, 1200), selGridInt, selStrings, 1);*
* GUI.EndScrollView();*
}
Save as Click.js
Create a new game object tag “Hands”
var cards : Array;
var handRect : Rect = Rect (0,290,270,190);
private var cardList : String[];
private var selGridInt : int = -1;
private var scrollPosition : Vector2 = Vector2.zero;

function Start(){
cardList = new String[1];
cardList[0] = “”;
cards = new Array();
}
function AddCards (cardsToAdd : String) {
* cards.Push(cardsToAdd);*
cardList = cards.ToBuiltin(String);
print(cardList[0]);
}
function OnGUI(){
* handRect = GUI.Window (1, handRect, DoMyHandWindow, “Hand”);*
}
function DoMyHandWindow (windowID : int) {
* scrollPosition = GUI.BeginScrollView (Rect (10,20,258,140), scrollPosition, Rect (0, 0, 230, 500));*
* if (cardList != null)*
{
selGridInt = GUI.SelectionGrid (Rect (0, 50, 235, 400), selGridInt, cardList, 1);
}
* GUI.EndScrollView();*
}

With a list:

// Assume cards is a List.<String> with all your cards.
var temp = List.<String>(cards);
cards.Clear();
while (temp.Count != 0) {
    var index = Random.Range(0, temp.Count);
    var card = temp[index];
    temp.RemoveAt(index);
    cards.Add(card);
}

Line by line, the algorithm does the following:

  1. Create a copy of the cards, called temp
  2. Clear the contents of cards
  3. While we have any temporary cards:
  4. Get a random card index
  5. Get the card for that index
  6. Remove the card from the temporary cards using the index
  7. Add the card to your set of cards

This will randomize quite nicely since it picks a card at random and places it in the list of cards. Other variants may shuffle the same card around multiple times causing low entropy. The down side is that it will consume more memory, but in your case it is negligable. Its possible to increase the entropy for the solution posted in your comment by increasing the number of iterations, which will add CPU overhead. This is my favorite shuffling algorithm for smaller sets of data since it is simple to follow and shuffles good.

One of many ways of doing it. Somebody may have to correct my syntax :wink:

public var shuffleCount:int = 10;
public var arrayOfStrings : String[] = new String["One", "Two", "Three", "Four", "Five"]; 

public function Shuffle(String[] collection) : void
{
	i1:int;
	i2:int;
	
	for(i:int = 0; i < shuffleCount; i++)
	{
		Random.seed = Random(0,1000);
		i1 = Random.Range(0, collection.length);
		i2 = Random.Range(0, collection.length);
		temp:String = collection[i1];
		collection[i1] = collection[i2];
		collection[i2] = temp;
	}
}

//call Shuffle when you want to shuffle cards
Shuffle(arrayOfStrings);