0

i have an array of objects that I'm trying to randomize and i was wondering how can i find the data of a random location in an array using Debug.log? this is mainly so i know if it is working right.

my Code:

    while(Deck.length != suffledDeck.length)
    {       
        var ranNum = Random.Range(1,Deck.length);

        suffledDeck.Add(Deck[ranNum]);

        Debug.Log(suffledDeck[ranNum]);

        //Debug.Log(suffledDeck[ranNum]);
    }
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Josh Banks
  • 31
  • 2
  • 9
  • You realize you are not really shuffling, right? See http://stackoverflow.com/questions/2450954/how-to-randomize-a-javascript-array for a good solution – Floris Mar 06 '13 at 23:01
  • See my answer here for another shuffling solution, http://stackoverflow.com/questions/15192614/javascript-how-to-stop-a-random-number-from-appearing-twice/15192668#15192668 – elclanrs Mar 06 '13 at 23:14
  • i just need to randomize it and how im doing it will work i just need to know how i can use the Debug.Log() with the array. – Josh Banks Mar 06 '13 at 23:22

1 Answers1

0

You can shuffle a array with this fonction:

//@ http://jsfromhell.com/array/shuffle [v1.0]
function shuffle(o){ //v1.0
    for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
    return o;
};
deck = shuffle(deck);

After that you can acces your array normally.

Thomas Durieux
  • 466
  • 2
  • 5
  • What is the type of suffledDeck. If it is a array you can use the push function and this function returns the position in the array. `var position = suffledDeck.push(Deck[ranNum]); Debug.Log(suffledDeck[position]);` – Thomas Durieux Mar 06 '13 at 23:37
  • i figured it out i was looking in the location of the random num not the current position i was in. – Josh Banks Mar 06 '13 at 23:55