Shuffle array elements (3 to end of array) in random order

Discussion in 'Javascript' started by Tuxedo, May 6, 2007.

  1. Tuxedo

    Tuxedo Guest

    I'd like to reorganize the third, fourth, fifth and sixth, as well as any
    elements thereafter in an array in random order:

    var a = new Array('first','second','third','fourth','fifth','sixth','etc')

    In other words, the first, second and third element should remain in
    position 0, 1 and 2, while the fourth, fifth and sixth, etc. should appear
    in random order.

    Can anyone recommend a method to do this?
    Tuxedo, May 6, 2007
    #1
    1. Advertising

  2. Tuxedo

    Lee Guest

    Tuxedo said:
    >
    >I'd like to reorganize the third, fourth, fifth and sixth, as well as any
    >elements thereafter in an array in random order:
    >
    >var a = new Array('first','second','third','fourth','fifth','sixth','etc')
    >
    >In other words, the first, second and third element should remain in
    >position 0, 1 and 2, while the fourth, fifth and sixth, etc. should appear
    >in random order.
    >
    >Can anyone recommend a method to do this?


    First you need to describe what you want in a way that isn't
    self-contradictory. Then you should read this newsgroup's FAQ,
    particularly the note box in this section:
    http://www.jibbering.com/faq/#FAQ4_22


    --
    Lee, May 6, 2007
    #2
    1. Advertising

  3. Tuxedo

    Tuxedo Guest

    Lee wrote:

    [...]

    > First you need to describe what you want in a way that isn't
    > self-contradictory. Then you should read this newsgroup's FAQ,
    > particularly the note box in this section:
    > http://www.jibbering.com/faq/#FAQ4_22


    Thank you for pointing me to that specific FAQ including those noteboxes
    with links to some pretty complex math!! I hope the answer hides somewhere
    in there....

    In which way do you mean my description is self-contradictory?

    To be more precise, after reorgnizing the latter part of the array, i.e.
    starting from 3 index, the intended result, or the properties of the new
    array, could be for example:

    'first','second','third','fourth','fifth','sixth','etc'
    or
    'first','second','third','fifth','fourth','sixth','etc'
    or
    'first','second','third','sixth','fifth','fourth','etc')
    or
    'first','second','third','etc','fourth','sixth','fifth'
    etc.

    The first 3 elements should remain in the position of the original array,
    while the remaining elements should be simply reshuffled in a random order,
    appearinging only once in any one position.

    What is best here? Would it be to create an array without the first 3
    elements and then add them into a randomly generated array of specified
    elements?
    Tuxedo, May 6, 2007
    #3
  4. Tuxedo

    Tuxedo Guest

    Randy Webb wrote:

    [...]

    > "I want my array in random order but I want to control what is or is not
    > random" is self-contradictory.


    That was not exactly what I meant, but what I did mean was for some but not
    all elements of an array to be reorganized in random order. But I think you
    understood by now that I was not referring to the entire array as such.

    > Yes.


    Thanks for the tip, which by the sound of it, I guess must be easier than
    randomly reorganise only a specified segment of the complete array.

    My question is therefore reduced and simplified to: How can I randomly
    reorganise the elements of the following array?

    var a = new Array('fourth','fifth','sixth','etc')
    Tuxedo, May 6, 2007
    #4
  5. Tuxedo

    Tuxedo Guest

    Tuxedo wrote:

    > Thanks for the tip, which by the sound of it, I guess must be easier than
    > randomly reorganise only a specified segment of the complete array.


    To answer myself, with some extra help from about.com
    (http://javascript.about.com/library/blsort2.htm) the following solution
    appears to work just fine:

    function randOrd(){
    return (Math.round(Math.random())-0.5);
    }

    var a = new Array('fourth','fifth','sixth','etc')
    a.sort(randOrd)
    a.unshift('first','second','third')
    Tuxedo, May 6, 2007
    #5
  6. Tuxedo

    Tuxedo Guest

    Lee wrote:

    [..]

    > In other words, the first, second and third element should remain ...


    Yes. Maybe there was some confusion with regards to my definition of the
    starting point.

    [..]

    > Look through those links for references to shuffling. You'll find
    > code that you can copy and paste. Once you're familiar with it,
    > you can decide whether to modify it slightly to begin at the third
    > (or is it fourth?) position, or whether you want to split it before
    > you shuffle and rejoin afterwards.


    Thank you for the tips!
    Tuxedo, May 6, 2007
    #6
  7. Tuxedo

    Lee Guest

    Tuxedo said:
    >
    >Lee wrote:
    >
    >[...]
    >
    >> First you need to describe what you want in a way that isn't
    >> self-contradictory. Then you should read this newsgroup's FAQ,
    >> particularly the note box in this section:
    >> http://www.jibbering.com/faq/#FAQ4_22

    >
    >Thank you for pointing me to that specific FAQ including those noteboxes
    >with links to some pretty complex math!! I hope the answer hides somewhere
    >in there....
    >
    >In which way do you mean my description is self-contradictory?


    You said:

    I'd like to reorganize the third, fourth, ...

    In other words, the first, second and third element should remain ...

    The "third" element appears both in the list to be reorganized and
    in the list to remain in place.

    Look through those links for references to shuffling. You'll find
    code that you can copy and paste. Once you're familiar with it,
    you can decide whether to modify it slightly to begin at the third
    (or is it fourth?) position, or whether you want to split it before
    you shuffle and rejoin afterwards.


    --
    Lee, May 6, 2007
    #7
  8. In comp.lang.javascript message <f1l099$34t$02$-online.com>,
    Sun, 6 May 2007 18:11:25, Tuxedo <> posted:
    >Tuxedo wrote:
    >
    >> Thanks for the tip, which by the sound of it, I guess must be easier than
    >> randomly reorganise only a specified segment of the complete array.


    Possibly easier; but not otherwise better.


    >To answer myself, with some extra help from about.com
    >(http://javascript.about.com/library/blsort2.htm) the following solution
    >appears to work just fine:
    >
    >function randOrd(){
    >return (Math.round(Math.random())-0.5);
    >}
    >
    >var a = new Array('fourth','fifth','sixth','etc')
    >a.sort(randOrd)
    >a.unshift('first','second','third')


    That method was discussed here some years ago, as an example of amusing
    but ludicrous programming.

    The specification of Array.sort calls for repeatable comparisons; the
    sort algorithm to be used is not defined, and may vary from browser to
    browser, version to version. With non-repeatable comparison, the result
    cannot be guaranteed : the sort algorithm might not even finish in some
    systems.

    An efficient shuffle of N elements takes time O(N), and uses O(N) calls
    of Random. The sort algorithm will take time at least about O(NlogN)
    calling Random O(2NlogN) times.


    The first question you should consider is whether you actually need a
    Shuffle. A Shuffle starts with all the elements present on some order
    and ends with them all on an independent order, probably in the same
    Array. You might be able to install the first three elements in direct
    order, then the rest in random order.

    For shuffle-in-place, it would be easier to keep the last three elements
    unmoved, by doing a standard shuffle but lying about the length. So,
    consider

    function Random(X) {
    return Math.floor(X*(Math.random()%1)) /* %1 : Opera */ }

    function ShuffleX(Q, x) { var R, T, J
    for (J=Q.length-1 -x ; J>0 ; J--)
    { R=Random(J+1) ; T=Q[J] ; Q[J]=Q[R] ; Q[R]=T }
    return Q }

    A = [1,2,3,4,5,6,7,8,9]
    A = A.reverse()
    ShuffleX(A, 3)
    A = A.reverse()

    In that, ShuffleX is the FAQ-cited standard (Knuth?) algorithm, modified
    by the addition of the parameter x.

    Now you can optimise by rewriting ShuffleX so that it works the other
    way round, and omit the reversals.

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

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
    news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr J R Stockton, May 6, 2007
    #8
  9. Tuxedo

    Tuxedo Guest

    Dr J R Stockton wrote:

    [..]

    > function Random(X) {
    > return Math.floor(X*(Math.random()%1)) /* %1 : Opera */ }
    >
    > function ShuffleX(Q, x) { var R, T, J
    > for (J=Q.length-1 -x ; J>0 ; J--)
    > { R=Random(J+1) ; T=Q[J] ; Q[J]=Q[R] ; Q[R]=T }
    > return Q }
    >
    > A = [1,2,3,4,5,6,7,8,9]
    > A = A.reverse()
    > ShuffleX(A, 3)
    > A = A.reverse()
    >
    > In that, ShuffleX is the FAQ-cited standard (Knuth?) algorithm, modified
    > by the addition of the parameter x.
    >
    > Now you can optimise by rewriting ShuffleX so that it works the other
    > way round, and omit the reversals.


    Not that I've seen the ludicrous about.com + unshift method fail on any
    browser I've tested it on so far, but hey, your solution certainly sound
    like the more sophisticated one!! Thanks for the detailed code example!
    Tuxedo, May 7, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    12
    Views:
    14,482
    Lee Ryman
    Apr 26, 2005
  2. Steve Holden

    Re: Implementaion of random.shuffle

    Steve Holden, Jul 16, 2007, in forum: Python
    Replies:
    15
    Views:
    513
    Neil Cerutti
    Jul 19, 2007
  3. globalrev
    Replies:
    4
    Views:
    746
    Gabriel Genellina
    Apr 20, 2008
  4. Jeff Moore

    Array.shuffle/Array.shuffle!

    Jeff Moore, Aug 24, 2008, in forum: Ruby
    Replies:
    6
    Views:
    147
    Alexei Broner
    Oct 9, 2008
  5. VK
    Replies:
    15
    Views:
    1,130
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page