Page 1 of 1
I NEED A UNIQUE NUMBER GENERATOR FUNCTION
Posted: Thu Jul 20, 2023 9:44 pm
by unixkd
Hi All
I need a function that can generate Unique number.
What I found close to it is ot4xb function cGenRndStr(10,.T.) but it generate alpha numeric strings
Thanks
Joe
Re: I NEED A UNIQUE NUMBER GENERATOR FUNCTION
Posted: Thu Jul 20, 2023 11:44 pm
by Piotr D
Hi Joe,
it depends on the scale of application of this uniqueness. If it's global, you need to use UUID. In Xbase++ you have UuidCreate() and UuidToChar() which convert binary uuid to characters (0-9 and a-f), then you need (self) convert a-f characters to value (with function ASC()) - but this will be very long.
If you need local (like in one application with concurrent users), you can create these value using for example combination of Date(), Seconds() and functions which return MAC address.
Regards
Piotr
Re: I NEED A UNIQUE NUMBER GENERATOR FUNCTION
Posted: Fri Jul 21, 2023 8:08 am
by rdonnay
If it needs to be unique, you can't use a random generator.
Is the uniqueness needed for a database record id?
Re: I NEED A UNIQUE NUMBER GENERATOR FUNCTION
Posted: Fri Jul 21, 2023 3:01 pm
by unixkd
Hi Roger
If it needs to be unique, you can't use a random generator.
Is the uniqueness needed for a database record id?
Not intend for database id. It is transaction document ID. I am thinking of something deriviable from Date()+Seconds()+Computer ID.
The computer ID must be SERVER ID for all connected users, something like that but I cant figure it out properly
Thanks
Joe
Re: I NEED A UNIQUE NUMBER GENERATOR FUNCTION
Posted: Fri Jul 21, 2023 3:24 pm
by unixkd
The unique number must be something like 1458763952 for 10 digits or 356498715465 for 12 digit number.
This ot4xb function cGenRndStr(10,.T.) generate id of 10 digits when 10 parameter is passed. The only problem is that it generates ALPHANUMERIC whereas I want only numeric.
Joe
Re: I NEED A UNIQUE NUMBER GENERATOR FUNCTION
Posted: Sat Jul 22, 2023 8:12 am
by jezda
Try
nNum:=RandomInt(0,9999999999) //--for 10 digit number
nNum:=RandomInt(0,999999999999) //--for 12 digit number
Re: I NEED A UNIQUE NUMBER GENERATOR FUNCTION
Posted: Sun Jul 23, 2023 3:17 am
by unixkd
Dear Jezda
I know that long time ago, it will NOT produce UNIQUE number at any given time. That is why Roger said that RAMDOM number generator cannot be used in his earlier reply.
Thanks
Joe
Re: I NEED A UNIQUE NUMBER GENERATOR FUNCTION
Posted: Sun Jul 23, 2023 11:35 pm
by SlavkoDam
It is transaction document ID. I am thinking of something derivable from Date()+Seconds()+Computer ID.
When you derive your string ID as above, you can convert it to HEX and that HEX to DEC, what will gives you a unique number. PowerUtl library contains functions for this.
Unique number can be also created with a cryptography HMAC based password generation function. The input values for that function will be you document ID and the number of digits in the result. The result is a unique string password containing only digits, which can be transformed to a number with the VAL() function. This algorithm is used in all bank mobile apps for generating one-time passwords for transactions approval. PowerCrp library contains this function.
Re: I NEED A UNIQUE NUMBER GENERATOR FUNCTION
Posted: Tue Jul 25, 2023 12:25 am
by k-insis
> It is transaction document ID. I am thinking of something deriviable from Date()+Seconds()+Computer ID.
Create a table with primary key , which goes from n ---> for 12 or more digits.
Before you use any random generated number, try it insert into that table (with some additional data to keep track/audit) and if insert fails
you have non unique.
If insert is allright, you have application wide unique number generator based on random numbers.
Problem solved. Any sql server will be capable of handling thousands of such queries per second.
There is fairly good random bytes generator (can be converted to int) in xb2net .
You might even create table with
CREATE TABLE SomethingUnique (ID001 UNIQUEIDENTIFIER DEFAULT NEWID() PRIMARY KEY,
NAME001 VARCHAR(100) )
which will provide uuid (which is a bit long for your use case)
Also:
https://learn.microsoft.com/en-us/windo ... tgenrandom
unixkd wrote: ↑Fri Jul 21, 2023 3:01 pm
Hi Roger
If it needs to be unique, you can't use a random generator.
Is the uniqueness needed for a database record id?
Not intend for database id. It is transaction document ID. I am thinking of something deriviable from Date()+Seconds()+Computer ID.
The computer ID must be SERVER ID for all connected users, something like that but I cant figure it out properly
Thanks
Joe