Q/A: Hoe haal ik vier willekeurige woorden uit een lijst in AS3?

Posted at March 07, 2011

You’ve found a dinosaur. This is one of my first posts about programming and the code quality is very poor, it is only here for archiving purposes.

Laatst kreeg ik de vraag hoe je het best een aantal willekeurige woorden uit een lijst haalt.

Hier heb ik even snel een klasse in elkaar gedraait die een array maakt, met daarin de top 50 films van IMDB. Hierna haal ik 4 willekeurige woorden uit dit array.

/** randomWords.as -- Mar 8, 2011
 *
 * Beschrijving: voorbeeld willekeurige woorden uit een array halen
 * 
 * @author Mike van Rossum
 *
 * Copyleft 2011, all wrongs reversed.
 */
package
{
    // Importeer benodigde classes
    import flash.display.Sprite;

public class randomWords extends Sprite
{
    // Variabele declaratie
    private var woordenLijst:Array;
    // Constructor functie
    public function randomWords()
    {
        woordenLijst = new Array(
            "The Shawshank Redemption (1994)",
            "The Godfather (1972)",
            "The Godfather: Part II (1974)",
            "The Good, the Bad and the Ugly (1966)",
            "Pulp Fiction (1994)",
            "Schindlers List (1993)",
            "12 Angry Men (1957)",
            "Inception (2010)",
            "One Flew Over the Cuckoo's Nest (1975)",
            "The Dark Knight (2008)",
            "Star Wars: Episode V - The Empire Strikes Back (1980)",
            "The Lord of the Rings: The Return of the King (2003)",
            "Seven Samurai (1954)",
            "Fight Club (1999)",
            "Goodfellas (1990)",
            "Star Wars: Episode IV - A New Hope (1977)",
            "Casablanca (1942)",
            "City of God (2002)",
            "The Lord of the Rings: The Fellowship of the Ring (2001)",
            "Once Upon a Time in the West (1968)",
            "Rear Window (1954)",
            "Raiders of the Lost Ark (1981)",
            "The Matrix (1999)",
            "Psycho (1960)",
            "The Usual Suspects (1995)",
            "The Silence of the Lambs (1991)",
            "Se7en (1995)",
            "It's a Wonderful Life (1946)",
            "Memento (2000)",
            "The Lord of the Rings: The Two Towers (2002)",
            "Toy Story 3 (2010)",
            "Sunset Blvd. (1950)",
            "Forrest Gump (1994)",
            "The Professional (1994)",
            "Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb (1964)",
            "Apocalypse Now (1979)",
            "Citizen Kane (1941)",
            "North by Northwest (1959)",
            "American Beauty (1999)",
            "American History X (1998)",
            "Taxi Driver (1976)",
            "Terminator 2: Judgment Day (1991)",
            "Saving Private Ryan (1998)",
            "Vertigo (1958)",
            "Alien (1979)",
            "Amélie (2001)",
            "WALL·E (2008)",
            "Spirited Away (2001)",
            "The Shining (1980)",
            "Paths of Glory (1957)"
        );

        var willekeurigWoord1:String = new String;
        var willekeurigWoord2:String = new String;
        var willekeurigWoord3:String = new String;
        var willekeurigWoord4:String = new String;

        var random:int = new int;
        random = Math.random()*50;
        willekeurigWoord1 = woordenLijst[random];

        random = Math.random()*50;
        willekeurigWoord2 = woordenLijst[random];

        random = Math.random()*50;
        willekeurigWoord3 = woordenLijst[random];

        random = Math.random()*50;
        willekeurigWoord4 = woordenLijst[random];

        trace(willekeurigWoord1);
        trace(willekeurigWoord2);
        trace(willekeurigWoord3);
        trace(willekeurigWoord4);
    }

    // Eigen functies
}

(Je moet de code in debug runnen omdat ik antwoorden alleen trace, je kan de strings willekeurigWoord1-4 nu in textfields stoppen of wat je er ook mee wilt doen.)

Houd er rekening mee dat een woord meerdere keren kan voorkomen.

Posted at March 07, 2011, under AS3.