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
I NEED A UNIQUE NUMBER GENERATOR FUNCTION
Re: I NEED A UNIQUE NUMBER GENERATOR FUNCTION
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
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
If it needs to be unique, you can't use a random generator.
Is the uniqueness needed for a database record id?
Is the uniqueness needed for a database record id?
The eXpress train is coming - and it has more cars.
Re: I NEED A UNIQUE NUMBER GENERATOR FUNCTION
Hi Roger
The computer ID must be SERVER ID for all connected users, something like that but I cant figure it out properly
Thanks
Joe
Not intend for database id. It is transaction document ID. I am thinking of something deriviable from Date()+Seconds()+Computer ID.If it needs to be unique, you can't use a random generator.
Is the uniqueness needed for a database record 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
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
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
Try
nNum:=RandomInt(0,9999999999) //--for 10 digit number
nNum:=RandomInt(0,999999999999) //--for 12 digit number
nNum:=RandomInt(0,9999999999) //--for 10 digit number
nNum:=RandomInt(0,999999999999) //--for 12 digit number
Re: I NEED A UNIQUE NUMBER GENERATOR FUNCTION
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
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
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.It is transaction document ID. I am thinking of something derivable from Date()+Seconds()+Computer ID.
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.
Best regards,
Slavoljub Damnjanovic
SD-SoftDesign, Alaska Software Technology Partner
https://www.sd-softdesign.com
https://www.sd-softdesign.rs
Slavoljub Damnjanovic
SD-SoftDesign, Alaska Software Technology Partner
https://www.sd-softdesign.com
https://www.sd-softdesign.rs
Re: I NEED A UNIQUE NUMBER GENERATOR FUNCTION
> 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
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
Not intend for database id. It is transaction document ID. I am thinking of something deriviable from Date()+Seconds()+Computer ID.If it needs to be unique, you can't use a random generator.
Is the uniqueness needed for a database record id?
The computer ID must be SERVER ID for all connected users, something like that but I cant figure it out properly
Thanks
Joe