Game Development Community

Randomizing arrays

by Johnathon · in Technical Issues · 11/28/2007 (7:27 pm) · 2 replies

I have an array that contains 52 items, can I randomize the items within that array using a pre-built method? or do i haft to do something like below

[code]
psuedo code
$Array[51]; //already filled
for (%count = 0; %count != 51; %count++)
{
%num = generate random number between 0 & 51
$newArray[0] = %num;
$array[%num] = ""
}
[/code

Only problem I see with this is I would haft to check if the %num is blank due to it accessing a blank element in the array.

Sorry, I am a .Net junkie and use the ArrayList which auto sorts things for me, and I have never needed to randomize something before (either in an array or not).

Thanks for any tips!

#1
11/28/2007 (9:34 pm)
I found something that worked for me, I found a previous post where a developer had used a simset to create his array, and then used getRandom to generate a number between 0 and %obj.getCount(), pulled the entry from the sim set into a copy of the simset and then deleted %obj from the original simset.

this is what I used.
for (%face = 0; %face != 4; %face++)
	{
		for (%value = 1; %value != 14; %value++)
		{
			%card = new t2dSceneObject(getCardName(%face, %value))
			{
				defaultconfig = CardDataBlock;
				cardFace = %face;
				cardValue = %value;
				imageMap = getImageName(%face, %value);
			};
			$Deck.Add(%card);
		}
	}
	
	//randomize the deck
	$DeckSorter = $Deck;
	$Deck = new simset();
	while ($DeckSorter.getCount() > 0)
	{
		%card = $DeckSorter.getObject(getRandom(0, $DeckSorter.getCount() - 1));
		$Deck.add(%card);
		$DeckSorter.Remove(%card);
	}
	
	for (%value = 0; %value != $Deck.getCount(); %value++)
	{
		%card = $Deck.getObject(%value);
		echo("Card: " @ getCardName(%card.cardFace, %card.cardValue));
	}

and the getCardName(%face, %value) method was created here
function getImageName(%face, %value)
{
	%img = "Cards";
	
	switch$ (%face)
	{
		case "0": //Hearts
			%img = %img @ "Hearts" @ %value @ $DeckBack;
		case "1": //clubs
			%img = %img @ "Clubs" @ %value @ $DeckBack;
		case "2": //Diamonds
			%img = %img @ "Diamonds" @ %value @ $DeckBack;
		case "3": //Spades
			%img = %img @ "Spades" @ %value @ $DeckBack;
	}
}

function getCardName(%face, %value)
{
	switch$ (%face)
	{
		case "0":
			%img = "Hearts" @ %value;
		case "1":
			%img = "Clubs" @ %value;
		case "2":
			%img = "Diamonds" @ %value;
		case "3":
			%img = "Spades" @ %value;
	}
}
and the echo produced a random deck of cards for me in the console :D
Card: Diamonds10
Card: Spades4
Card: Hearts9
Card: Hearts3
Card: Clubs7
Card: Diamonds1
Card: Diamonds8
Card: Clubs13
Card: Hearts7
Card: Clubs8
Card: Clubs1
Card: Spades6
Card: Hearts1
Card: Clubs3
Card: Spades7
Card: Diamonds9
Card: Clubs2
Card: Diamonds13
Card: Hearts13
Card: Hearts8
Card: Diamonds6
Card: Clubs6
Card: Spades12
Card: Clubs4
Card: Hearts10
Card: Clubs12
Card: Diamonds5
Card: Diamonds3
Card: Spades9
Card: Diamonds7
Card: Hearts2
Card: Spades8
Card: Clubs10
Card: Spades5
Card: Hearts11
Card: Spades13
Card: Hearts12
Card: Diamonds11
Card: Spades1
Card: Clubs9
Card: Diamonds2
Card: Spades2
Card: Clubs11
Card: Spades10
Card: Diamonds12
Card: Spades11
Card: Hearts4
Card: Hearts6
Card: Clubs5
Card: Diamonds4
Card: Hearts5
Card: Spades3
#2
11/30/2009 (5:00 am)
Here's a small function I wrote for this. I thought I would find something more efficient in the forums, but I did not. This is the best I could come up with.
function IO::randomizeList(%list)
{
	%randomList = "";
	while (true)
	{
		setRandomSeed();
		%max = getWordCount(%list) - 1;
		if (%max <= -1) break;
		%randomIndex = getRandom(0, %max);
		%randomList = %randomList @ " " @ getWord(%list, %randomIndex);
		%list = removeWord(%list, %randomIndex);
	}
	
	return trim(%randomList);
}