This should be easy but I can't get it!

This is a discussion on This should be easy but I can't get it! within the filemaker forums in Other Databases category; 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...

Go Back   Database Forum > Other Databases > filemaker

Database Forums

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-24-2008, 05:16 AM
Default This should be easy but I can't get it!

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

Reply With Quote
  #2  
Old 08-24-2008, 05:52 AM
Default Re: This should be easy but I can't get it!

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.


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
Reply With Quote
  #3  
Old 08-24-2008, 06:26 AM
Default Re: This should be easy but I can't get it!

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.


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
Reply With Quote
  #4  
Old 08-24-2008, 07:35 AM
Default Re: This should be easy but I can't get it!

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)
Reply With Quote
  #5  
Old 08-24-2008, 08:08 PM
Default Re: This should be easy but I can't get it!

On 2008-08-24 05:26:50 -0400, dempson-at-actrix.gen.nz (David Empson) said:

> 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.

>
> 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

Reply With Quote
Reply


Thread Tools
Display Modes



All times are GMT -4. The time now is 05:08 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Integrated by bbpixel2008 :: jvbPlugin R1013.368.1

Search Engine Friendly URLs by vBSEO 3.1.0
vB Ad Management by =RedTyger=
In an effort to better serve ads to our visitors, cookies are used on Mydatabasesupport.com. For more information, check out our Privacy Policy.