If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

help using javascript using a random file

so what I am trying to do is create a function does the following three things each time it is run:
1. pick a random sound file from an array and play it twice
2. remove the played file from the list in the array
3. when there is only one file left, restore all original values to the array so that the process can start over.

at this point I don't know what my mistake is but the way it is coded here, it's not doing anything at all... I'm assuming I've made several syntax mistakes.

ok i think i see how you switched around the order of operations there... but how does "function() {return 0.75 - Math.random()}" scramble the array? Math.random() just calls picks a random number between 0 and 1 right? thats what i was taught. (That's why I'm confused about the 0.75 part) what if I wanted to work with a much larger number of values, like putting over 100 different elements in the array?

That said this works in IE and Opera on my machine because I have the quicktime player installed if I didnít have the plugin I wouldnít be able to play the sounds at all, it is worth noting that not every browser has an audio plugin installed.

i get 20-something more than half the time, which is not even close to 100, and "0" performs worse than "1".
or try "if(r2[3]===1){ hits+=1; }", which gives me an avg of about 150, when it should be 100.

Also of note is that even random()-0.5 does not actually result in a randomized array; early indices are more likely to still be up front when the sort finishes. for a 10 digit array, the odds of slot zero becoming slot 9 are ~4k/1, not 1/10 !
try testing with 0.5, looking for 1 at slot #9, you see that it's VERY disproportionately scarce compared to looking for 1 at slot #3...

I suspect that's what you were attempting to compensate for; indeed more reversed sorts "look" more jumbled.

"0.5" does give better results than 0.75, at least at first...

The bottom line is that .sort() is not very good at "randomizing" arrays, even if you see it used a lot for that purpose.
i have to admit, i swallowed that crap without thinking and used it for a while. Until i noticed some of my random pattern generators look obviously non-random, and i dug into code to catch the culprit; [].sort to randomize...

Carefully avoiding a wiseguy comment about Math.random()'s actual randomness, i will assure that collecting elements at "random()*.length" into a new array is FAR more random.

if it just has to be "different" instead of "random", [].sort() will almost always re-arrange the array you give it.

Last edited by rnd me; 09-30-2012 at 07:29 PM.

Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com