Discussions
Categories
- 197K All Categories
- 2.5K Data
- 546 Big Data Appliance
- 1.9K Data Science
- 450.8K Databases
- 221.9K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 552 MySQL Community Space
- 479 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.1K ORDS, SODA & JSON in the Database
- 556 SQLcl
- 4K SQL Developer Data Modeler
- 187.2K SQL & PL/SQL
- 21.4K SQL Developer
- 296.3K Development
- 17 Developer Projects
- 139 Programming Languages
- 293K Development Tools
- 110 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 158 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.2K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 19 Java Essentials
- 162 Java 8 Questions
- 86K Java Programming
- 81 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 205 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 468 LiveLabs
- 39 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 175 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 233 Portuguese
generate non duplicate integers

I want to generate 7 digit integers.
These integers should never be duplicate even if I re-run program multiple times.
is there any java solution to generate these numbers?
Answers
-
You may want to look at
https://stackoverflow.com/questions/2982748/create-a-guid-in-java
-
okay..But this does not give 7 digit integer. Is there any way to get 7 digit integer ?
<span class="kwd" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #101094;">import</span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;"> java</span><span class="pun" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">.</span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">util</span><span class="pun" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">.</span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">UUID</span><span class="pun" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">;</span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;"><br/><br/></span><span class="kwd" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #101094;">public</span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;"> </span><span class="kwd" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #101094;">class</span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;"> </span><span class="typ" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #2b91af;">RandomStringUUID</span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;"> </span><span class="pun" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">{</span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;"><br/> </span><span class="kwd" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #101094;">public</span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;"> </span><span class="kwd" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #101094;">static</span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;"> </span><span class="kwd" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #101094;">void</span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;"> main</span><span class="pun" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">(</span><span class="typ" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #2b91af;">String</span><span class="pun" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">[]</span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;"> args</span><span class="pun" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">)</span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;"> </span><span class="pun" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">{</span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;"><br/> UUID uuid </span><span class="pun" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">=</span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;"> UUID</span><span class="pun" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">.</span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">randomUUID</span><span class="pun" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">();</span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;"><br/> </span><span style="color: #2b91af; font-weight: inherit; font-family: inherit; font-style: inherit;">int </span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">randomUUID </span><span class="pun" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">=</span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;"> uuid</span><span class="pun" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">.</span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">clockSequence</span><span class="pun" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">();</span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;"><br/> </span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;"><br/> </span><span class="pun" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">}</span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;"><br/></span><span class="pun" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;"></span>
<span class="pun" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">}</span>
-
These integers should never be duplicate even if I re-run program multiple times.
Not possible.
okay..But this does not give 7 digit integer. Is there any way to get 7 digit integer ?
Don't be silly.
There are only ~ 10 million positive integers so if you 're-run program multiple times' and generate 10 million numbers each time there are GUARANTEED to be duplicates.
Or if you re-run it 10 million times you will get duplicates.
When there are limits/constraints on the size of the value the ONLY way to be sure there are no duplicates is to check new numbers against a list of old ones that have previously been generated.
Instead of asking about what you think is your solution why don't you tell us WHAT PROBLEM you are trying to solve.
-
Its related to OpenLDAP new group creation.
Please see this screen
https://tr1.cbsistatic.com/hub/i/2016/12/15/0f19df06-811c-48e8-91e6-b0c9696395a4/phpldapadming.jpg
in this image, you see there is a gidNumber input........ that value needs to be unique.
I want to generate a unique 7 digit number for gidNumber input.
any java solution to this ?
Thanks
-
any java solution to this ?
Yes - and I already told you what that solution is:
When there are limits/constraints on the size of the value the ONLY way to be sure there are no duplicates is to check new numbers against a list of old ones that have previously been generated.
Or you can:
1. create a list of ALL 7 digit numbers with each number having a 'Y' or 'N' flag indicating if it has been used.
2. use the next number in the list whose flag is 'N' and change the flag to 'Y'.
An obviously contrived example will illustrate the problem:
1. every day you want to use a generated unique value to decide if you should wear your blue tie to work
2. the unique value must be either HEADS or TAILS
Please tell us how you could flip a coin every morning and GUARANTEE that you will ALWAYS get a unique value that hasn't been used before.
See the problem?
-
1. create a list of ALL 7 digit numbers with each number having a 'Y' or 'N' flag indicating if it has been used.2. use the next number in the list whose flag is 'N' and change the flag to 'Y'.
This solution is not acceptable.
I'm not going to create a List of ALL 7 digit integers and manage Y/N everytime. This is extra overhead. Also I dont want to persis any of these for checking later.
There were other similar solutions which I did not consider too.
Example,
1. Use database sequence numbers
2. serialization counter in a file and increase every time by 1 when I generate a new number
These were overhead to my REST calls.
-
This solution is not acceptable.I'm not going to create a List of ALL 7 digit integers and manage Y/N everytime. This is extra overhead. Also I dont want to persis any of these for checking later.
It is certainly your choice whether you want to be the one to implement the requirement.
But you have just ruled out the ONLY solutions that can meet the requirement you gave us.
There were other similar solutions which I did not consider too.Example,1. Use database sequence numbers2. serialization counter in a file and increase every time by 1 when I generate a new numberThese were overhead to my REST calls.
#1 - sequence numbers are NOT guaranteed to be gap-free - the first number might be 1 and the next could be 21.
#2 - you have to persist the info somewhere so you have to do it in a file of some sort - even storing it in a database stores it in a file.
ANYTHING you do will be 'overhead' - you can't get something for nothing.