Jump to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Need help with calc to find unique random number

Featured Replies

Hello

I have a file with a field "rangebegin" and a field "rangeend" I want to have a field called "usednumbers" and a field called "NUMBER"

I am in filemaker 7;

I would like to have a script that would pick a new number from within the range of "rangebegin & rangeend" make sure that each time it picks a new number. Can someone help me with this one.

Thanks very very much

Stann

I've been working on a similar thing for slotting staff into random training groups. This is the algorithm I've come up with:

Set Field [ "Number" , "Int(Random*RangeEnd) + RangeBegin" ] //Get a random number in the range

Loop

If ["isempty(RangeEnd) or RangeEnd < RangeBegin" ] //Error

Exit Script

End If

Exit Loop If [ "PatternCount(gNumbersUsed, Number)" ] //A empty slot found

Set Field [ "Number" , "Number +1" ] //Try the next number up

If [ "Number > RangeEnd" ] //Reached the end of the range, start over at the start of the range

Set Field [ "Number" , "RangeBegin" ]

End If

End Loop

Set Field [ "gNumbersUsed", "gNumbersUsed & " par.gif"& Number" ]

When it tries a number that has already been used, it sequentially tries the next greater number. This doesn't feel as random as trying another random number, but it's more efficient.

Assuming you only want integer values, for the random, you could also use:

MyRandomNumber = Round( Random * ( Table_1::RangeEnd - Table_1::RangeBegin ) ; 0 ) + Table_1::RangeBegin

Attached is a quick file to illustrate.

zippy.zip

  • Author

Thank you both , I have a question though

How can I in response to Zincb's example keep it from using the same number twice.

That is very important in this case. I need help with this.

thanks

Stan

  • Author

also , If my range is 1-300 is there a way to keep a three number figure like

003,055, 104, 298, 007 , 018, 214 etc.

Another approach you might consider is to use a table with say, 999 records. Each record has a field for your number, which should be a TEXT field if you want to use leading zeroes. A second field would contain a corresponding serial number, which we will use for a lookup.

What your script would do is generate a random number in the range, and then use that number to do a lookup on the table above. If no match is found, it selects the next higher value. Then clear the serial number field so that the next lookup won't use it. I.e., clearing the serial number marks the number as "used."

Sorry, I didn't fully comprehend your question in my last reply. Attached is a modified file that gives you up to 9 leading zeros, and will allow you to select just a few numbers at a time.

randomizer.zip

  • Author

I am sorry zincb but I can't enter any numbers in the file you attached. I don't know what is wrong. I go to rangeend or rangebegin and It will not allow me to add numbers?

I learn something new every day!

Evidently FM7 defaults to a Guest account with read-only rights, which have to be changed to allow "modify" access. More default security, I suppose, but I sort of liked only having to secure things that were vital. We are Borg; the Collective will adapt.

Please try the attached file.

randomizer2.zip

  • Author

I am not sure that is it. I went into privs and put the account to full access but it is not letting me enter anything into the file.

Stann

Not the solution in here just a question that can bring some light in this situation...

Okay so you want to assign unique random numbers to a number of records... but is it like you have 200 records and want to appoint the numbers 000 to 200 in random order, or is it more like 50 records and appoint a random value from 0000 to 9999..

Cause in situation 1, you might want to look to a reversed approach, adding 001 to a random record and then adding 002 and so on.... cause the chance the random generator generates a valid integer in situation 1 near the end of the cycle is about 0,5%... imagine what the searchtime would be with 10.000+ integers and the same amount of records.

stann said:

I am not sure that is it. I went into privs and put the account to full access but it is not letting me enter anything into the file.

Stann

I may be getting distracted from the original post, but this is getting very curious. Did you download the second file I posted (#106879 - 05/25/04 06:58 AM) or are you talking about the first one?

In the second file, I set the [Guest] account to "Full Access", so you should have been able to do anything, up through complete destruction. One thought -- I created the file on a Windows 2000 box -- what platform are you running on?

Create an account or sign in to comment

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.