On Apr 1, 9:36 am, ab5tract <[EMAIL PROTECTED]> wrote:
> Problem:
> I am creating a "teaser page" for a site that is coming soon. We have
> a list of 20 sentences that describe the site. We want to select 5 of
> those, at random and without redundancy, and display them in a div.
> The display part seems easy enough, and should look something like
> this:
>
> $(document).ready() {
>   $("quote_box").append(quote_array)
>
> }
>
> However, I cannot seem to find any information about randomly loading
> an array y out of an array x, in jQuery or otherwise.

If you can generate the quoteArray, and convert an array of strings
into the DOM object(s) you want, something like this may help (if you
want to maintain order, it will require a fairly simple change):

var quote_array = ['a', 'b', 'c', 'd', 'e', 'f',
  'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o'];

function genRandomSet(src, n) {

  var dat = src.concat();
  var max = dat.length-1;
  var set = [];

  n = (n<max)? n : max;

  while (n--) {
    set.push(dat.splice(Math.random()*dat.length|0, 1));
  }
  return set;
}

alert( genRandomSet(quote_array, 5).join('\n') );


--
Rob

Reply via email to