Forum Stats

  • 3,838,749 Users
  • 2,262,397 Discussions
  • 7,900,750 Comments

Discussions

generate non duplicate integers

user575089
user575089 Member Posts: 466
edited Oct 20, 2018 11:18AM in Java Programming

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?

rpc1morgalr

Answers

  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited Oct 16, 2018 7:52AM
  • user575089
    user575089 Member Posts: 466
    edited Oct 16, 2018 8:23AM

    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>

  • Unknown
    edited Oct 16, 2018 11:46AM
    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.

    rpc1morgalr
  • user575089
    user575089 Member Posts: 466
    edited Oct 19, 2018 7:08AM

    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

  • Unknown
    edited Oct 19, 2018 10:59AM
    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?

    rpc1
  • user575089
    user575089 Member Posts: 466
    edited Oct 20, 2018 8:03AM
    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.

  • Unknown
    edited Oct 20, 2018 11:18AM
    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.