Sehr kurze Snippet: shuffle () in Javascript und Actionscript

In PHP gibt es ein praktisches Feature namens shuffle() , die ein Array mischen (siehe ermöglicht sehr kurze Trick: Nehmen zufällige Elemente aus einem Array in PHP ). Eine große Version für Javascript fand ich es hier . Unten ist der Code leicht überarbeitet:

1
2
3
= function ( ) { Array. Prototype. Shuffle = function () {
var j , x , i = this . length ; i ; j = Math. floor ( Math. random ( ) * i ) , x = this [ -- i ] , this [ i ] = this [ j ] , this [ j ] = x ) ; for (var j, x, i = diese Länge; i; j = Math Etage (Math. random () * i), x = this [- i], diese [i] = this [j], diese.. [j] = x);
}

al posto di parseInt() , in quanto da test effettuati è risultato estremamente più veloce, almeno sulla mia macchina e con FireFox 3. Im Vergleich zum ursprünglichen I erweitert das Objekt Array und eingefügt Math.floor() statt parseInt() , als Folge der durchgeführten Tests ist extrem schneller, zumindest auf meinem Rechner und FireFox 3.

In ActionScript 3.0, habe ich es in die Klassenzimmer MatrixArray :

1
2
3
4
5
6
7
8
/ **
* Shuffle in Zufallsmatrix
*
* /
shuffle ( ) : void { public function shuffle (): void {
/ / Dank http://jsfromhell.com/array/shuffle
var j , x , i = __len ; i ; j = Math . floor ( Math . random ( ) * __len ) , x = this [ -- i ] , this [ i ] = this [ j ] , this [ j ] = x ) ; for (var j, x, i = __ len, i; j = Math . Etage ( Math . random () * __ len), x = this [- i], diese [i] = this [j], diese [j ] = x);
}

4 Kommentare zu: ""

  1. 2. April 2009 Sehr kurze Snippet: Actionscript verlängern ein Array mit der Methode shuffle () | Undolog.com :

    [...] Bereits darüber, wie Sie die Methode shuffle () in Javascript und Actionscript Umsetzung gesprochen. Ich erkannte, tuttaavia, nicht darauf hingewiesen haben, dass auch in der Lage, Actionscript [...] ist

  2. 2. April 2009 DaveOnCode :

    Dies ist meine eigene Lösung: http://www.daveoncode.com/2009/01/08/implementing-arrayshuffle-in-actionscript/

    Es ist nur 3 Zeilen Code und es funktioniert gut :)

  3. 2. April 2009 Giovan Battista Fazioli :

    @ DaveOnCode: sehr kurze Schnipsel :) thx

  4. 26. Oktober 2010 Objective-C: Unterklassen und Kategorien | Undolog.com :

    [...] Wie in sehr kurze Schnipsel gesehen: shuffle () in Javascript und Actionscript. [...]

Hinterlasse einen Kommentar

TAG XHTML PERMITS: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> Code-Eingabe:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL