I NEED A UNIQUE NUMBER GENERATOR FUNCTION

This forum is for eXpress++ general support.
Post Reply
Message
Author
User avatar
unixkd
Posts: 575
Joined: Thu Feb 11, 2010 1:39 pm

I NEED A UNIQUE NUMBER GENERATOR FUNCTION

#1 Post 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

Piotr D
Posts: 131
Joined: Mon Jul 28, 2014 1:26 am
Location: Poznań, Poland

Re: I NEED A UNIQUE NUMBER GENERATOR FUNCTION

#2 Post 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

User avatar
rdonnay
Site Admin
Posts: 4774
Joined: Wed Jan 27, 2010 6:58 pm
Location: Boise, Idaho USA
Contact:

Re: I NEED A UNIQUE NUMBER GENERATOR FUNCTION

#3 Post by rdonnay »

If it needs to be unique, you can't use a random generator.

Is the uniqueness needed for a database record id?
The eXpress train is coming - and it has more cars.

User avatar
unixkd
Posts: 575
Joined: Thu Feb 11, 2010 1:39 pm

Re: I NEED A UNIQUE NUMBER GENERATOR FUNCTION

#4 Post 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

User avatar
unixkd
Posts: 575
Joined: Thu Feb 11, 2010 1:39 pm

Re: I NEED A UNIQUE NUMBER GENERATOR FUNCTION

#5 Post 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

jezda
Posts: 17
Joined: Thu Mar 28, 2019 4:41 am

Re: I NEED A UNIQUE NUMBER GENERATOR FUNCTION

#6 Post by jezda »

Try
nNum:=RandomInt(0,9999999999) //--for 10 digit number
nNum:=RandomInt(0,999999999999) //--for 12 digit number

User avatar
unixkd
Posts: 575
Joined: Thu Feb 11, 2010 1:39 pm

Re: I NEED A UNIQUE NUMBER GENERATOR FUNCTION

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

User avatar
SlavkoDam
Posts: 111
Joined: Wed Apr 27, 2022 10:12 am
Location: Negotin, Serbia
Contact:

Re: I NEED A UNIQUE NUMBER GENERATOR FUNCTION

#8 Post 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.
Best regards,

Slavoljub Damnjanovic
SD-SoftDesign, Alaska Software Technology Partner
https://www.sd-softdesign.com
https://www.sd-softdesign.rs

k-insis
Posts: 120
Joined: Fri Jan 28, 2011 4:07 am

Re: I NEED A UNIQUE NUMBER GENERATOR FUNCTION

#9 Post 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

Post Reply