Sehr kurze Auszug: shuffle () in Javascript und Actionscript

In PHP gibt es eine praktische Funktion namens shuffle() , dass Sie ein Array-Mix (siehe ermöglicht sehr kurze Trick: Nehmen Sie zufällige Elemente aus einem Array in PHP ). Eine hervorragende Version des Javascript fand ich hier . Leicht überarbeitete Sie den nachfolgenden Code:

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 = dieser Länge, i, j = Math floor (Math.E random () * i), x = this.. [- i], das [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 zu den ursprünglichen habe ich das Objekt erweitert Array und legte Math.floor() anstelle von parseInt() , weil aus unseren Tests war sehr schnell, zumindest auf meinem Rechner und mit Firefox 3.

In ActionScript 3.0, benutzte ich das Klassenzimmer MatrixArray :

1
2
3
4
5
6
7
8
/ **
* Mix in einer zufälligen Matrix
*
* /
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 floor (. Math random () * __len), x = this. [- i], das [i] = this [j], das [j ] = x);
}

4 Kommentare: "Sehr kurze Auszug: shuffle () in Javascript und Actionscript"

  1. 2. April 2009 Sehr kurze Auszug: Actionscript Verlängerung eines Arrays mit der Methode shuffle () | Undolog.com :

    [...] Bereits darüber, wie die Methode shuffle () in Javascript und Actionscript umsetzen gesprochen. Ich erkannte, tuttaavia, nicht darauf hinzuweisen, dass Actionscript Lage 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 Giovambattista Fazioli :

    @ DaveOnCode: sehr kurze Schnipsel :) thx

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

    Shuffle () in Javascript und Actionscript: [...] Wie in der sehr kurzen Ausschnitt gesehen. [...]

Hinterlasse einen Kommentar

XHTML TAG PERMIT: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <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