In a message dated 1/27/05 4:46:45 AM, 
[EMAIL PROTECTED] writes:

>
>Message: 19
>Date: Thu, 27 Jan 2005 19:31:39 +1000
>From: "D.Rothe" <[EMAIL PROTECTED]>
>Subject: Random #'s
>To: <use-revolution@lists.runrev.com>
>Message-ID: <[EMAIL PROTECTED]>
>Content-Type: text/plain;  charset="iso-8859-1"
>
>Hi All, 
>I am using the following code to generate a field of 6 random numbers.
>
>repeat 5 times --generate list of 5 random numbers between 1-10
>put random(10) & "," after tRnum 
>end repeat
>delete last char of tRnum --delete last comma
>put tRnum into fld "numbers"
>
>this works fine but some numbers are repeated in the field, e.g (1,2,3,1,5)
>How can I include only unique numbers?
   I can think of a couple of options, offhand...

# Brute Force and Massive Ignorance!
# generate randoms and throw out the ones that are already selected

function UniqueRnds1
  put "" into Rezult
  repeat
    put the random of 10 into Fred
    if ("," & Fred & ",") is not in Rezult then
      put Fred into item (1 + the number of items in Rezult) of Rezult
    end if
    if the number of items in Rezult = 5 then return Rezult
  end repeat
end UniqueRnds1

# Slightly More Elegant!
# make a list and scramble it

function UniqueRnds2
  # build list
  put "" into Rezult
  repeat with K1 = 1 to 10
    put K1 into item K1 of Rezult
  end repeat
  # scramble list
  repeat 10
    put the random of 10 into Fred
    put item Fred of Rezult into George
    delete item Fred of Rezult
    put George & "," before Rezult
  end repeat
  return Rezult
end UniqueRnds2

   Hopefully, at least one of this pair will serve your needs...
_______________________________________________
use-revolution mailing list
use-revolution@lists.runrev.com
http://lists.runrev.com/mailman/listinfo/use-revolution

Reply via email to