use-revolution Digest, Vol 16, Issue 87

Cubist at Cubist at
Thu Jan 27 09:28:34 EST 2005

In a message dated 1/27/05 4:46:45 AM, 
use-revolution-request at writes:

>Message: 19
>Date: Thu, 27 Jan 2005 19:31:39 +1000
>From: "D.Rothe" <drothe at>
>Subject: Random #'s
>To: <use-revolution at>
>Message-ID: <000501c50453$01f97710$a6fe1dd3 at p4c2ghz>
>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
    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...

More information about the Use-livecode mailing list