| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
|
| how to use a calculation to return a Random number between 0001 and 9999.... the problem is I want the leading zeros, if they come up. thanks JP -- “Travel is fatal to prejudice, bigotry and narrow-mindedness.” -Mark Twain |
|
#2
|
| On Sun, 24 Aug 2008 04:16:29 -0400, Jonathan Peirce >how to use a calculation to return a Random number between 0001 and 9999.... >the problem is I want the leading zeros, if they come up. Set the calc result to be text instead of number and the leading zeros should be preserved. hope this helps -- FW FileMaker Pro 8.5 Advanced on Windows XP Pro SP2 FileMaker Server 8.0 on Windows 2003 Server R2 |
|
#3
|
| Jonathan Peirce > how to use a calculation to return a Random number between 0001 and 9999.... > the problem is I want the leading zeros, if they come up. Use this formula in your calculation, with a result type of text: Right (Int ( Random * 9998.999 ) + 10001; 4) Explanation: Random generates a random fractional number between 0.0000 and 1.000 (inclusive). Your desired range is 1 through 9999, so a simple approximation is to multiply the result of Random by 9999, use Int() to discard the fraction, then add 1. This works for most random numbers but has a catch: an exact random number of 1.0 would produce a result of 10000, which outside your target range. This is corrected by changing the multiplier to be slightly less than 9999. The product of (Random * 9998.999) is then in the range 0.000 through 9998.999, and Int truncates it to 0 through 9998. Adding 1 shifts the range to 1 through 9999, with reasonably even distribution. To get the leading zero digits, add 10000 and then use Right(...; 4) to convert the number to text and only keep the last four digits, which will be 0001 through 9999. -- David Empson dempson-at-actrix.gen.nz |
|
#4
|
| On Sun, 24 Aug 2008 04:16:29 -0400, Jonathan Peirce wrote: > how to use a calculation to return a Random number between 0001 and 9999.... > the problem is I want the leading zeros, if they come up. right("0000"&int(random()*9999+1), 4) |
|
#5
|
| On 2008-08-24 05:26:50 -0400, dempson-at-actrix.gen.nz (David Empson) said: > Jonathan Peirce > >> how to use a calculation to return a Random number between 0001 and 9999.... >> the problem is I want the leading zeros, if they come up. > > Use this formula in your calculation, with a result type of text: > > Right (Int ( Random * 9998.999 ) + 10001; 4) > > Explanation: > > Random generates a random fractional number between 0.0000 and 1.000 > (inclusive). > > Your desired range is 1 through 9999, so a simple approximation is to > multiply the result of Random by 9999, use Int() to discard the > fraction, then add 1. This works for most random numbers but has a > catch: an exact random number of 1.0 would produce a result of 10000, > which outside your target range. > > This is corrected by changing the multiplier to be slightly less than > 9999. The product of (Random * 9998.999) is then in the range 0.000 > through 9998.999, and Int truncates it to 0 through 9998. Adding 1 > shifts the range to 1 through 9999, with reasonably even distribution. > > To get the leading zero digits, add 10000 and then use Right(...; 4) to > convert the number to text and only keep the last four digits, which > will be 0001 through 9999. Thanks everyone, that does the trick. And I learned about 'Right'! -- “Travel is fatal to prejudice, bigotry and narrow-mindedness.” -Mark Twain |
![]() |
| Thread Tools | |
| Display Modes | |