How do I randomly select a string from an array in swift?

Select a random string from an array

  • Possible Duplicate: http://stackoverflow.com/questions/4550505/javascript-getting-random-value-from-an-array Could someone help me on this topic? I've got this code. var textArray = [ 'song1.ogg', 'song2.ogg' ] audioElement.setAttribute('src', textArray); How can I randomly get one of those strings into my audio element? Would be glad if someone can help....

  • Answer:

    var textArray = [ 'song1.ogg', 'song2.ogg' ]; var randomNumber = Math.floor(Math.random()*textArray.length); audioElement.setAttribute('src', textArray[randomNumber]);

Mausoleum at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

var randomIndex = Math.floor(Math.random() * textArray.length); var randomElement = textArray[randomIndex];

sberry

http://blog.mastykarz.nl/jquery-random-filter/ Here is a way you can do it with a JQuery Extension.

Joseph Shanak

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.