473,386 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Generating unique

I have made a stored procedure, containing this part for generating a unique
reference number.

SET i = 0;
REPEAT
SET i = i + 1;
SELECT RAND()
INTO reference
FROM SYSIBM.SYSDUMMY1;
SET p_reference = ref_prefix || SUBSTR(CAST(reference AS CHAR(12)),3);
SELECT cardno
INTO cardno
FROM HTTP.TB00008 WHERE reference = p_reference;
UNTIL (cardno IS NULL OR i = 1000)
END REPEAT;
IF (i = 1000) THEN
SIGNAL c_failed_reference
SET MESSAGE_TEXT = 'Unique reference could not be generated in a 1000
attempts';
END IF;

Unfortunately it happens from time to time, that it fails generating a
unique reference in a 1000 attempts.
Is the RAND() function not unique enough, and what can I do else ?
Thanks in advance

Poul
Nov 12 '05 #1
6 5516
You try a 1000 times to find a gap in your card no? Are the rows missing
or is cardno truly NULL in teh row? If the later, what about this:
How about this:
SELECT reference INTO p_reference
FROM
(SELECT reference,
rownumber() over () as rn,
count(*) over cnt
FROM HTTP.TB00008
WHERE cardno IS NULL) AS X
WHERE rn = integer(rand() * cnt) + 1

This will find you a random gap.

Cheers
Serge
Nov 12 '05 #2
Small correction:
SELECT reference INTO p_reference
FROM
(SELECT reference,
rownumber() over () as rn,
count(*) over cnt
FROM HTTP.TB00008
WHERE cardno IS NULL) AS X, TABLE(VALUES(rand())) AS R(rand) WHERE rn = integer(rand * cnt) + 1


Cheres
Serge
Nov 12 '05 #3
> You try a 1000 times to find a gap in your card no? Are the rows missing
or is cardno truly NULL in teh row? If the later, what about this:
How about this:
SELECT reference INTO p_reference
FROM
(SELECT reference,
rownumber() over () as rn,
count(*) over cnt
FROM HTTP.TB00008
WHERE cardno IS NULL) AS X
WHERE rn = integer(rand() * cnt) + 1


No, I'm making a unique reference that hasn't already been used, at the time
of inserting.
cardno is a not null field generated in a triggered sequence.
So if the select cardno from http.tb0008 where reference = "generated value"
gives no result,
I have found one.

The escaping after a 1000 attempts is a temporary one to prevent looping
forever, when it fails finding a unique one.
I'm just surprised that the select rand() is giving the same result a 1000
times in a row.
Cheers
Poul

Nov 12 '05 #4
Poul Møller Hansen wrote:
I have made a stored procedure, containing this part for generating a unique
reference number.


Does it need to be a random number? Why not use a sequence? If you
have a big 'stride' which is mutually prime with the number of entries
in the sequence (eg - if the sequence ranges over 1,000,000 to
1,000,000,000, then 31,572,493 is prime relative to 999,000,000 and
will generate numbers such as 32,572,493 followed by 64,144,986 and
95,717,479 and 127,289,972 and so on. The 'mutual prime' property
ensures that every value in the range will be used eventually.

--
Jonathan Leffler #include <disclaimer.h>
Email: jl******@earthlink.net, jl******@us.ibm.com
Guardian of DBD::Informix v2003.04 -- http://dbi.perl.org/
Nov 12 '05 #5
AK
Poul,

the easiest way to get unique numbers is to use a sequence, which
gives you consecutive numbers, i.e. 1,2,3,...

if you need unique numbers to be not consecutive, you might want to
play with swapping bytes and / or bits.

For instance, get numbers 0x00000001, 0x00000002, 0x00000003,...
and swap bytes to 0x01000000, 0x02000000, 0x03000000,...

You might also read Donald Knuth's book, where he describes basics of
random numbers generating
Nov 12 '05 #6
To measure the number of repeats in a small rand() sample of
size 10,000, one can do something like this on UDB:

create table samp ( i integer ) ;

insert into samp
with temp(n) as ( values( 1 )
union all
select n + 1 from temp
where n < 10000 )
select n from temp ;

commit ;

select count( * ) from samp ;
select count( distinct rand(1) ) from samp ;
select count( distinct rand(5) ) from samp ;
select count( distinct rand(7) ) from samp ;

The desired result is 10,000 from each select.

-Gordon

"Poul Møller Hansen" <ph@pbnet.dk> wrote in message
news:HQ**************@news.get2net.dk...
I have made a stored procedure, containing this part for generating a
unique reference number.

SET i = 0;
REPEAT
SET i = i + 1;
SELECT RAND()
INTO reference
FROM SYSIBM.SYSDUMMY1;
SET p_reference = ref_prefix || SUBSTR(CAST(reference AS CHAR(12)),3);
SELECT cardno
INTO cardno
FROM HTTP.TB00008 WHERE reference = p_reference;
UNTIL (cardno IS NULL OR i = 1000)
END REPEAT;
IF (i = 1000) THEN
SIGNAL c_failed_reference
SET MESSAGE_TEXT = 'Unique reference could not be generated in a 1000
attempts';
END IF;

Unfortunately it happens from time to time, that it fails generating a
unique reference in a 1000 attempts.
Is the RAND() function not unique enough, and what can I do else ?
Thanks in advance

Poul

Nov 12 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: lallous | last post by:
Hello, This code works fine when 'size' is less than 32768 however when size is bigger this function never returns. Can't find out why?! If I break into the code I can see that 'i' is 32768.......
4
by: Justin Lebar | last post by:
Sorry about the huge post, but I think this is the amount of information necessary for someone to help me with a good answer. I'm writing a statistical analysis program in ASP.net and MSSQL7 that...
6
by: Stan | last post by:
Is it possible to hash a 100 bytes string to a integer? I found a few .NET classes for that such as Sha1Managed.ComputeHash but they return bytes. I am just not sure about the idea of converting...
7
by: eric.gagnon | last post by:
In a program randomly generating 10 000 000 alphanumeric codes of 16 characters in length (Ex.: "ZAZAZAZAZAZAZ156"), what would be an efficient way to ensure that I do not generate duplicates? ...
4
by: Mark | last post by:
BEGINNER QUESTION I have a table which has a compound primary key consisting of two columns. One of these columns is a foreign key which is generated in another table by an identity. I want...
2
by: Lukas Meusburger | last post by:
i have a problem auto-assigning mnemonics when dynamicly generating menues. in some cases, two memorie entries get the same mnemonic key assigned because the words begin with the same letter....
1
by: Velhari | last post by:
Hi, I am a beginner. Please tell me, For generating Random Numbers, Initially why we are going for seed method. And another question is that, I want to print unique random number how to print by...
14
by: Steven D'Aprano | last post by:
I have an application that will be producing many instances, using them for a while, then tossing them away, and I want each one to have a unique identifier that won't be re-used for the lifetime...
2
by: Gary Hasler | last post by:
Does anyone have any suggestions on generating GUID (Globally Unique Identifier) tags with php? They would need to be in the format 4402bd8a-cd51-40ea-99d7-b510e89e344b Specifically this is for...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.