13 Replies Latest reply on Jan 25, 2016 10:37 PM by greatgrey

# Generate a Random unique number and Perform Script

Hello everyone,

I have two concatenated odd questions as follows.

1) The first question: I need to fill a field with 20 repetitions of numeric values from 1 to 20, which is fine and I can do that.

But the question is how to make each number unique, i.e. one unique number from 1 to 20 in random order for each of the 20 repetitions

2) The second question: how to perform a specific script based on each value of each repetition.

For example if the first repetition is 5, run Script 5.... and so on for each repetition

Victor

• ###### 1. Re: Generate a Random unique number and Perform Script

Victor --

>2) The second question: how to perform a specific script based on each value of each repetition.

>For example if the first repetition is 5, run Script 5.... and so on for each repetition

This is easier to answer.  If you set up a 'master script' and your 20 subscripts, then you can call the master script (iteratively if you need to) to grab a repetition number and:

If ( \$value = 1)

Perform Script ( "One" )

Else If ( \$value=2)

Perform Script ( "Two")

etc.etc.

There are only 20 possibilities, so it's not too bad to write them all out.

>1) The first question: I need to fill a field with 20 repetitions of numeric values from 1 to 20, which is fine and I can do that.

>But the question is how to make each number unique, i.e. one unique number from 1 to 20 in random order for each of the 20 repetitions

My calculator tells me that if you were to take all possible permutations of 20 things taken 20 at a time, you have 2.43 x 10^18 possibilities.

I guess I'd set up a script to create a new record with the values of 1-20 randomly assigned (as you siad you could), and then check to see if this particular permutation already exists.

For that check, you could set up a calculated field that is the concatenation of the 20 repetitions as a single string and use that string as a key to a self-related table.  If there is more than one matching record, then this particular permutation already exists and you can delete this record and try again.

Given the number of permutations, it is very likely that you can create quite a few records without ever having a duplicate.  I assume you don't need millions of these records, just a few hundred at most.  You could even create a condition to stop after getting 100 unique permutations.

Hope this helps,

Drew Tenenholz

• ###### 2. Re: Generate a Random unique number and Perform Script

One way is to fill your repeats with the numbers 1-20, the goto repeat1 get random 20, swap repeat1 with repeat(random) then goto repeat 2 and do the same, and repeat for all 20 locations. and yes a swap with itself is allowed. Also you can run through the whole swap cycle more than once to help mix it up.

• ###### 3. Re: Generate a Random unique number and Perform Script

As of FM13v2, there is syntax for performing script in URL

"fmp://\$/" & Get ( Filename ) & "?script=" & GeAsURLEncoded ( "yourScriptName" )

Not work on runtime or multi-version installation.

• ###### 4. Re: Generate a Random unique number and Perform Script

Hello Drew,

what you suggested is exactly what I did btw my purpose is to run not only the script related to a specific repetition but all the 20 scripts related to each value of each repetition in random order.

One way I could think is to repeat the process of the first repetition for each of the 20 repetitions, I was just wondering if there is anything faster and more "elegant".

• ###### 5. Re: Generate a Random unique number and Perform Script

Hello greatgrey,

interesting suggestion.

Let me check if you mean: calculate each of the 20 repetitions with 20 different scripts instead of having a self repeating calculated filed ?

• ###### 6. Re: Generate a Random unique number and Perform Script

try this one.

see attach.

how it works:

you have a help table with 20 records, with 2 significant fields: ID going from 1 to 20 and UUID, text.

The script fills the UUID field with Get(UUID), which acts as a random number. Then it sorts the 20 records based upon that field, which results in a random shuffling of the records. ListOf grabs the IDs, delivering a shuffled list of the 20 ID's. This is good if you need the result as a list.

You also get it in the calculated repeating field, of course, if that's your beer.

the second part is opinable, instead of running one of 20 scripts you should IMHO run just one script, passing the value as parameter and bowing the script to do things based upon the parameter.

• ###### 7. Re: Generate a Random unique number and Perform Script

Calculation for 20 repetition

Let ( [

n = Get ( CalculationRepetitionNumber ) ;

r = Int ( Random * ( 21 - n ) ) + 1 ;

\$ns = Case ( n = 1 ; "ABCDEFGHIJKLMNOPQRST" ; \$ns ) ;

c = Middle ( \$ns ; r ; 1 ) ;

\$ns = Substitute ( \$ns ; c ; "" )

] ;

Code ( c ) - Code ( "A" ) + 1

)

If there is a bad, using \$variable in it.

• ###### 8. Re: Generate a Random unique number and Perform Script

No not what I meant at all.

This file is from FMA 13.

Version 1 is what I meant.

Version 2 is what I understand as what you described for setting up your random data.

Nothing I know would make calling scripts any easier.

• ###### 9. Re: Generate a Random unique number and Perform Script

Hi siplus, nice job as always But for part 2 of my question I think I cannot manage with one single script (or at least I can't imagine how to do it).

Thanks user19752, I will try that !

Hi great grey, the one you suggest is a great approach ! Easy and fast, I am going to use it for the first part of my problem !!!

For the second part of my question the problem is linking a specific number in each repeating field with a specific script to perform without having a very long script filled with "If" and "Else If" which bring to some 400 lines (20 possibilities to be analysed by 20 different scripts)

I wish there was a function like Perform Script based on a variable....I searches inside other discussion but seems none is working in multiplatform solutions

• ###### 10. Re: Generate a Random unique number and Perform Script

vroncagli wrote:

I wish there was a function like Perform Script based on a variable....I searches inside other discussion but seems none is working in multiplatform solutions

if you are willing to use a free plugin, here it is:

Functions · GoyaPtyLtd/BaseElements-Plugin Wiki · GitHub

• ###### 11. Re: Generate a Random unique number and Perform Script

If your data is in 20 separate records and are calling the same 20 scripts just use a loop.

Not all details just basic idea.

Loop

Loop

\$runscript = Array[loopcounter]

If \$runscript = 1

preform scrip1

else if \$runscript = 2

preform scrip2

etc.

end if

end loop if

end loop

next record

end loop

• ###### 12. Re: Generate a Random unique number and Perform Script

hello greatgrey

yes, you suggestion sound it can be used.

In details what I am doing is adapting the shuffle array file you shared calculating in the repeating field the 20 different unique possibilities from 1 to 20.

Then I am adapting you latest suggestion to use that repeating field instead of 20 separate records performing a specific script based on the value of each repeating value.

I tried with a repeating field of 5 and it work!

Thank You !

Victor

• ###### 13. Re: Generate a Random unique number and Perform Script

Glad that I could be of some help.

The random array is just an old algorithm that has been used for decades to shuffle a deck of cards.