none
Scramble a list of numbers RRS feed

  • Question

  • I want to scramble the numbers 2 thru 17 in an array.  I can do it in a very kludgy manner by generating a random number between 2 and 17 inclusive, check to see if it is already in the list then either add it to the list or generate a new number to check.  Ugly.  There is probably a really slick way that I have forgotten several times and simply cannot remember.  Anybody have a slick little solution?
    Thursday, April 30, 2009 7:36 PM

Answers

  • I have converted Husker41's code to Small Basic syntax. The array names should probably be changed to something more meaningful, but I have left them the same for comparison.

    I also changed the assignment loop to
    For Z = 2 To (SIZE + 1)
    and the final output loop to
    For Z = 1 To SIZE
    so that it would still work if SIZE was changed.

    ----------------------------------------------------------------------------------------------------------------------------------------------

    SIZE = 16
    For Z = 2 To (SIZE + 1)
      Array.SetValue("A", Z-1, Z)
    EndFor
    '-------------[ Collection Shuffle ]------------------
    '
    '     In:     A() data to be shuffled
    '              SIZE of list
    '     Out:  X() shuffled data;    A() trashed
    '
    D = SIZE
    For J = 1 To SIZE
      R = Math.GetRandomNumber(D)  'random, 1 to size
      Array.SetValue("X", J, Array.GetValue("A", R))  'assign the value
      Array.SetValue("A", R, Array.GetValue("A", D))  'fill the hole
      D = D-1                                         'decrement the data choices
    EndFor
    For Z = 1 To SIZE
      TextWindow.WriteLine(Array.GetValue("X", Z))
    EndFor

    Friday, May 1, 2009 10:23 AM

All replies

  • In good old-fashioned GW-BASIC, from
     http://social.msdn.microsoft.com/Forums/en-US/smallbasic/thread/838ea03f-0abf-47fe-9b7a-a9fbe457da38


    100 RANDOMIZE
    110 DIM X(20),A(20)
    120 SIZE = 16
    130 FOR Z = 2 TO 17
    140 A(Z-1)=Z
    150 NEXT Z
    160  '-------------[ Collection Shuffle ]------------------
    170  '
    180  '     In:     A() data to be shuffled
    190  '              SIZE of list
    200  '     Out:  X() shuffled data;    A() trashed
    210  '
    220  D = SIZE
    230  FOR J = 1 TO SIZE
    240      R = INT(RND*D) +1                 'random, 1 to size
    250      X(J) = A(R)                                'assign the value
    260      A(R) = A(D)                               'fill the hole
    270      D = D-1                                     'decrement the data choices
    280  NEXT J
    290 FOR Z = 1 TO 16
    300 PRINT X(Z),
    310 NEXT Z
    320 END
    Thursday, April 30, 2009 8:05 PM
  • Mine is also inelegant but mimics an actual person doing the task

    Numbers = 0

    For I = 2 To 17
      Array.SetValue(Numbers, I, I)
      'print list
      Textwindow.Write(Array.GetValue(Numbers, I) + " ")
    EndFor

    Textwindow.WriteLine("")

    For I = 1 To 1000
      'select two indexes in the array
      ArrayNumber1 = Math.GetRandomNumber(16) + 1
      ArrayNumber2 = Math.GetRandomNumber(16) + 1 'there is no checking for different index number but it won't hurt

      'store arraynumber1 value in a temp variable
      TempNumber = Array.GetValue(Numbers, ArrayNumber1)
     
      'move arraynumber2 value to arraynumber1
      Array.SetValue(Numbers, ArrayNumber1, Array.GetValue(Numbers, ArrayNumber2))
     
      'move arraynumber1 value (stored in tempnumber) to arraynumber2
      Array.SetValue(Numbers, ArrayNumber2, TempNumber)
    EndFor

    'print a list
    For I = 2 To 17
      Textwindow.Write(Array.GetValue(Numbers, I) + " ")
    EndFor

    Thursday, April 30, 2009 8:22 PM
  • I have converted Husker41's code to Small Basic syntax. The array names should probably be changed to something more meaningful, but I have left them the same for comparison.

    I also changed the assignment loop to
    For Z = 2 To (SIZE + 1)
    and the final output loop to
    For Z = 1 To SIZE
    so that it would still work if SIZE was changed.

    ----------------------------------------------------------------------------------------------------------------------------------------------

    SIZE = 16
    For Z = 2 To (SIZE + 1)
      Array.SetValue("A", Z-1, Z)
    EndFor
    '-------------[ Collection Shuffle ]------------------
    '
    '     In:     A() data to be shuffled
    '              SIZE of list
    '     Out:  X() shuffled data;    A() trashed
    '
    D = SIZE
    For J = 1 To SIZE
      R = Math.GetRandomNumber(D)  'random, 1 to size
      Array.SetValue("X", J, Array.GetValue("A", R))  'assign the value
      Array.SetValue("A", R, Array.GetValue("A", D))  'fill the hole
      D = D-1                                         'decrement the data choices
    EndFor
    For Z = 1 To SIZE
      TextWindow.WriteLine(Array.GetValue("X", Z))
    EndFor

    Friday, May 1, 2009 10:23 AM