5 Replies Latest reply on Jun 6, 2006 12:07 PM by Newsgroup_User

    RANDOM issue

    Level 7
      Hi,

      I have a field member that has several lines of information like this:
      Item1
      Item2
      Item3
      Item4 .... and so on

      I need to write a script that will pull 3 random items out of that last
      without repeating themselves. How would I go about doing this?

      Thanks in advance for your help in this matter!


        • 1. Re: RANDOM issue
          a¿ex
          quote:

          Originally posted by: Newsgroup User
          I need to write a script that will pull 3 random items out of that last
          without repeating themselves. How would I go about doing this?


          on GetRandomLines theText, numDraws
          numLines = theText.line.count
          li = []
          repeat with n = numLines
          li.add(n)
          end repeat

          result = []
          repeat with n = 1 to numDraws
          thisDraw = random(count(li))
          result.add(text.line[thisDraw])
          li.deleteAt(thisDraw)
          if count(li) < 1 then exit repeat -- just to be sure, in case you want to draw more lines, than are in the text :-)
          end repeat

          return result

          end

          now if you want three lines of text xy

          put GetRandomLines(xy, 3)


          HTH
          • 2. Re: RANDOM issue
            Level 7
            Thank you for your help!

            One other issuse... so now I have a list of three items. I need to output
            them back into a text member like this:
            Item 1, Item2, Item3

            The trick, I guess is on the last item, not to put a "," after it... so
            there would only be a "," between items 1 & 2

            Thanks again for your help!




            "a?ex" <webforumsuser@macromedia.com> wrote in message
            news:e63rqr$m4l$1@forums.macromedia.com...
            > <blockquote>quote:<br><hr><i>Originally posted by: <b><b>Newsgroup
            > User</b></b></i>
            > I need to write a script that will pull 3 random items out of that last
            > without repeating themselves. How would I go about doing
            > this?<hr></blockquote>
            >
            > on GetRandomLines theText, numDraws
            > numLines = theText.line.count
            > li = []
            > repeat with n = numLines
            > li.add(n)
            > end repeat
            >
            > result = []
            > repeat with n = 1 to numDraws
            > thisDraw = random(count(li))
            > result.add(text.line[thisDraw])
            > li.deleteAt(thisDraw)
            > if count(li) < 1 then exit repeat -- just to be sure, in case you want
            > to
            > draw more lines, than are in the text :-)
            > end repeat
            >
            > return result
            >
            > end
            >
            > now if you want three lines of text xy
            >
            > put GetRandomLines(xy, 3)
            >
            >
            > HTH
            >
            >


            • 3. Re: RANDOM issue
              Level 7
              > Thank you for your help!
              >
              > One other issuse... so now I have a list of three items. I need to
              output
              > them back into a text member like this:
              > Item 1, Item2, Item3
              >
              > The trick, I guess is on the last item, not to put a "," after it... so
              > there would only be a "," between items 1 & 2
              >
              > Thanks again for your help!

              member("someTextMember").item[1] = "Item1"
              member("someTextMember").item[2] = "Item2"
              member("someTextMember").item[3] = "Item3"

              You'll probably want to put it into a loop or something, though I don't know
              exactly what your situation is. Just make sure your itemDelimiter is set to
              ",". It is by default, but if you've changed it for some reason, it will
              use whatever you changed it to.


              • 4. Re: RANDOM issue
                Level 7
                Actually you want that delete line to be deleteOne, not deleteAt. You
                want to remove the item that matches the random selection, not the item
                in the location of the random selection.

                --
                Rob
                _______
                Rob Dillon
                Adobe Community Expert
                http://www.ddg-designs.com
                412-243-9119

                http://www.macromedia.com/software/trial/
                • 5. Re: RANDOM issue
                  Level 7
                  My mistake, in this case deleteAt is correct.


                  based on a?lex's code:

                  ------------
                  on getRandomLines theText,numDraws,newTextMember
                  -- empty the new text member...
                  member(newTextMember).text = ""
                  -- an empty list to hold a sequential list of numbers...
                  li = []
                  -- an empty list to temporarily hold the strings that are picked from
                  the first text member...
                  newList = []
                  -- get the total number of lines to work with
                  numLines = member(theText).line.count
                  -- build a list of the line numbers that can be used...
                  repeat with n = 1 to numLines
                  li.add(member(theText).line[n])
                  end repeat
                  -- test to be sure that you are using a legal number of items to pull
                  out...
                  if numDraws <= numLines then
                  -- loop to grab the number of lines of text that you want
                  repeat with n = numDraws down to 1
                  -- pick a random number from the list of line numbers...
                  thisDraw = random(li.count)
                  -- add that line of text to the list
                  newList.add(li[thisDraw])
                  -- remove that number from the list...
                  li.deleteOne(thisDraw)
                  end repeat
                  -- loop through the grabbed lines of text and place them in a new
                  text member with spaces in between
                  repeat with n = 1 to numDraws-1
                  member(newTextMember).text = member(newTextMember).text &
                  newList[n] & Space
                  end repeat
                  -- add the last line
                  member(newTextMember).text = member(newTextMember).text &
                  newList[numDraws]
                  -- clear the temporary list
                  newList = []
                  else
                  -- show an alert if you've entered a bad number for numDraws...
                  alert("too many lines to retrieve")
                  end if
                  end
                  -------

                  and then call it like this:

                  getRandomLines "textMemberName",3,"newTextMemberName"

                  you can substitute a "," for the space if you like.

                  --
                  Rob
                  _______
                  Rob Dillon
                  Adobe Community Expert
                  http://www.ddg-designs.com
                  412-243-9119

                  http://www.macromedia.com/software/trial/