Random but unique

Rob Cozens rcozens at pon.net
Sat Jan 11 15:05:01 EST 2003


>So i use the random function to get an ID out of the first variable  and put
>it into a new variable. This new variable is a separated by returns, just as
>the first one.
>But to avoid the same ID more than once in this new list, I used a
>repeat-loop which first chechs if the new ID isn't allready in the list
>before adding it.
>To do this I use the "is not in" statement, but when "111" is the new list,
>the "is not in" ignores the value "1" and "11" as well...
>
>How can I get an unique but random number from a variable?

Hi Tom,

I can suggest a general approach; but can't get too specific without 
knowing the exact format of the text lines.

What I would attempt at first try is to see if it is possible to use 
the offset function instead of "is not in".  At first blush, it seems 
you have the same problem here: offset("1","xx111xxx") & 
offset("11","xx111xxx") are also non-zero.

What you do to deal with this is to include the delimiters that come 
before and after the ID in the string.

Suppose your list is organized:

Id,field 2,field 3,<return>

You could script something like...

if offset(return&newId&comma,return&yourText) <> 0 then answer 
"Duplicate Id" -- the return prefixed to yourText is needed in case 
the id is in the first line

If you list is organized:

field 1,Id,field 3,<return>

if offset(comma&"111"&comma,return&yourText) <> 0 then answer 
"Duplicate Id"; However, if there are more than three fields per line 
one would have to be certain that no non-Id interior field could 
match the comma, id, comma sequence.

For completeness:

field 1,field 2,Id<return>

if offset(comma&newId&return,yourText&return) <> 0 then answer 
"Duplicate Id" -- the return appended to yourText is needed in case 
the id is in the last line & the last line has no terminating return.

-- 

Rob Cozens
CCW, Serendipity Software Company
http://www.oenolog.com/who.htm

"And I, which was two fooles, do so grow three;
Who are a little wise, the best fooles bee."

from "The Triple Foole" by John Donne (1572-1631)



More information about the use-livecode mailing list