Jump to content
Salesforce and other SMB Solutions are coming soon. ×

Unique Random Numbers


summer

Recommended Posts

I came across your web page and thought someone may be able to help me.

 

I'm simply trying to have FileMaker generate a list of random numbers from 1 to 25 so that there are no repeated numbers. I would like a new set of numbers upon each start up (or a button event would work for me too).

 

In BASIC or PASCAL (the only languages I've used) it was a simple process of:

assigning a string variable all the numbers from 1 to (N) {010203 etc}

I generated a random number (Int(rand * N)+1,

I would locate number in the string variable and assign it to a new list.

I then reassigned the original string variable to the Left$ up to the number selected and concatenated it with the Right$ just past the number in the original string thus shrinking the the original string variable

Then reset the random multiplier (N=N-1) e.g., Int(rand *24)+1, Int(rand *23)+1, etc

I would loop back through this process until the original String variable contained only the last unselected number, i.e., N=1.

From here it is a simple matter of converting each "string number" in the new list to a real number.

 

Perhaps there is a better way to do all this but the main issue for me is how can I get FileMaker Pro. v6 (OS X.2) to produce 25 unique random numbers from 1 to 25? Your help and guidance would be greatly appreciated.

Link to comment
Share on other sites

Hi Summer

 

Welcome to the café!

 

You can do this with a scripted process as follows with two fields - RandomString (global text field), and RandomSource (global text field). RandomString is where your string of random numbers ends up and RandomSource is used as the source of those numbers.

 

Here is the script with comments:

 

#Initialise the fields

Set Field ["RandomString", """"]

Set Field ["RandomSource", ""01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 "]

#Enter a loop

Loop

#Exit if one number is left in the source (put less than or equal to symbol instead of .lte.)

Exit Loop If ["Length(RandomSource .lte. 3"]

#Add to the string a random two character number from the source

Set Field ["RandomString", "RandomString & Middle(RandomSource, Int(Random*Length(RandomSource)/3) * 3 + 1, 2)"]

#Substitute the two character number and trailing space out of the source (substitute with null)

Set Field ["RandomSource", Substitute(RandomSource, Right(RandomString, 2) & " ", "")

End Loop

#Set the last remaining source number onto the string

Set Field ["RandomString", ""RandomString & Left(RandomSource, 2)"]

 

And that will do it!

I have also set it up in another script to be more fancy, allowing the user to determine how many random numbers to generate up to a maximum of say 25.

 

After the script, you will end up with a string like:

06250107082117121911130509031016142422230415201802

 

Which is then easy to break up into numbers by stripping the last two digits as a number. It depends where you want to use them though - you didn't say.

Link to comment
Share on other sites

Hi summer,

 

and welcome to the Café!

 

If your random number range is relatively small, this would also work.

 

After you input the upper limit of the range from 1 to "N", (into the global "g_desired record count") the script creates "N" number of records, each with a unique random integer within the specified range.

 

# This script creates a user specified number of records,

# each with a unique random integer in the "random number" field.

# User has already specified a value for g_desired record count."

# “Create an initial record and set the random number field to a

# random integer between 1 and g_desired record count.”

New Record/Request

Set Field [ random number, Int(Random* g_desired record count + 1) ]

# “Now loop through the creation of the remaining records.”

Loop

# “exit the outer loop when Status(CurrentRecordCount) = g_desired record count”

Exit Loop If [ Status(CurrentRecordCount) = g_desired record count ]

# “Create the next record”

New Record/Request

Loop

# “Set global gn1 to a random integer between 1 and g_desired record count”

Set Field [ gn1, Int(Random* g_desired record count + 1) ]

# “Exit the inner loop if there is no record already assigned the value in gn1,

# using the self relationship "gn1 | random number" to perform the test

#(the "constant" field used in the test is simply an indexed calc field

#of type Number with a value of 1),

#and don't allow any records to be numbered "0" (zero)"

Exit Loop If [ not IsValid(self gn1 | random number::constant) and gn1 0 ]

End Loop

# “set the random number field to the value of gn1”

Set Field [ random number, gn1 ]

End Loop

 

Here's the script without all of the comments:

 

New Record/Request

Set Field [ random number, Int(Random* g_desired record count + 1) ]

Loop

Exit Loop If [ Status(CurrentRecordCount) = g_desired record count ]

New Record/Request

Loop

Set Field [ gn1, Int(Random* g_desired record count + 1) ]

Exit Loop If [ not IsValid(self gn1 | random number::constant) and gn1 0 ]

End Loop

Set Field [ random number, gn1 ]

End Loop

 

The script takes about 1/2 second to create 25 records, and about 7 seconds to create 1,000 records on a Powerbook G4-667, OS X.2.6 and FM 6.0.4

 

A demo file is available here. It contains my method and also a similar but somewhat more efficient method suggested by Bob Weaver.

 

Good luck!

Link to comment
Share on other sites

Thanks Jim,

 

I received two replies and I can definately use both. This is my first experience int seeking "outside" help for FileMaker. I'm pleasantly astonished at the timeliness... WOW! My daughter teaches 3rd grade and has asked me to develop a custom test generator. I planned on having FileMaker go to a different randomly selected record to display a question and record the student response. Your solution looks like it fits perfectly.

 

Kudos to you, Jim.

David M. Summer

Link to comment
Share on other sites



×
×
  • Create New...

Important Information

Terms of Use