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!
[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!
#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);
}
Torque Owner Johnathon
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