2Q : Condensing an array; how random works.

Giganews Newsgroups
Subject: 2Q : Condensing an array; how random works.
Posted by:  Dr John Stockton (spam@merlyn.demon.co.uk)
Date: Sun, 13 Jul 2003

Q1 :

Given an array such as might have been generated by

        var A = [,,2,,,,4,,,6,,,8,,,]

is there a highly effective way of reducing it to  [2,4,6,8]  - i.e.
removing the undefineds and shifting the rest down?

        A.sort().slice(0,n)  // would do it, but sorts; and the number
of active elements must be known.

I know, of course, about explicitly looping through all of A and testing
for undefined.

        A.join('#').split(/#+/)        // looks inefficient

Q2 :

Is anything exact known about the internal mechanism of Math.random() ?
Do later browsers provide access to the seed value?

--
© John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk  DOS 3.3, 6.20; Win98. ©
Web  <URLhttp://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via  <URLhttp://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS  <URLhttp://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.

Replies