In message <11**********************@e3g2000cwe.googlegroups. com>, Fri,

10 Nov 2006 19:36:42, mrtaka79 <mr******@gmail.comwrites

...

The only thing I want to add is to randomize the pictures/links that

show up.

...

Firstly, ISTM, one must consider what "randomise" of pictures 1 to N

should mean, if in a show of infinite length.

I see two limiting cases :

A) The pictures are chosen at random, independently of what went before.

The chances of a repeat, in any one display, of the previous picture

are 1 in N; the chances of a repeat, in a total of N draws, must

exceed 50%.

B) The pictures are assigned, once and for all, a random order; and that

order is repeatedly cycled through. Picture P is either always or

never followed by picture Q.

Then there's

C) One can draw the pictures in random order, show them all, and repeat

draw-and-show. There's then a 1 in N chance of a repeat at the end

of each cycle, and ISTM a 2 in N chance of a picture being repeated

with a single one in between.

Now consider

D) Deal, once and for all, the set of N picture-numbers into an array X.

Then, for each display, select at random one of the first M entries;

show it; and move its entry to the end of the array, allowing other

entries to move forwards. Easily coded, of course.

If M==N, we have Case A.

If M==1, we have Case B.

If M==N-K, repeats of a picture occur at intervals containing at least K

other pictures.

But, as in Case A but not Case B, it is still possible (if Random is

perfectly random, which it is not), for an unlucky picture never to be

shown; there is no upper limit on the interval between showings of a

given picture.

QUERY : is there a simple elegant method including the advantage of Case

D but fixing the defect?

Of course, one could maintain for each entry in the first M a countdown

of how long it had been there, starting at M+J, and select an entry when

its count reached 0, treating it as if it had been chosen as before. But

I see no better way of doing that than a loop through all M decrementing

an array element ...

Test Code for Case D :

function Slides() { var N = 8, M = 4, Q = Deal(N), A = [], j, r, k, t

document.writeln("Q: ", Q)

for (j=0 ; j<64 ; j++) { r = Random(M) ; t = A[j] = Q[r]

for (k=r+1 ; k<N ; k++) Q[k-1] = Q[k] ; Q[N-1] = t }

document.writeln("A: ", A.join(""), "\nQ: ", Q) }

Can the k loop be *better* written with array methods ?

It's a good idea to read the newsgroup and its FAQ. See below.

--

(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6

<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript

<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.

<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.