Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7563681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:43:31+00:00 2026-05-30T13:43:31+00:00

I have a code that should get unique string(for example, d86c52ec8b7e8a2ea315109627888fe6228d) from client and

  • 0

I have a code that should get unique string(for example, “d86c52ec8b7e8a2ea315109627888fe6228d”) from client and return integer more than 2200000000 and less than 5800000000. It’s important, that this generated int is not random, it should be one for one unique string. What is the best way to generate it without using DB?

Now it looks like this:

did = "d86c52ec8b7e8a2ea315109627888fe6228d"
min_cid = 2200000000
max_cid = 5800000000
cid = did.hash.abs.to_s.split.last(10).to_s.to_i
if cid < min_cid
  cid += min_cid
else
  while cid > max_cid
    cid -= 1000000000
  end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:43:32+00:00Added an answer on May 30, 2026 at 1:43 pm

    Here’s the problem – your range of numbers has only 3.6×10^9 possible values where as your sample unique string (which looks like a hex integer with 36 digits) has 16^32 possible values (i.e. many more). So when mapping your string into your integer range there will be collisions.

    The mapping function itself can be pretty straightforward, I would do something such as below (also, consider using only a part of the input string for integer conversion, e.g. the first seven digits, if performance becomes critical):

    def my_hash(str, min, max)
      range = (max - min).abs
      (str.to_i(16) % range) + min
    end
    
    my_hash(did, min_cid, max_cid) # => 2461595789
    

    [Edit] If you are using Ruby 1.8 and your adjusted range can be represented as a Fixnum, just use the hash value of the input string object instead of parsing it as a big integer. Note that this strategy might not be safe in Ruby 1.9 (per the comment by @DataWraith) as object hash values may be randomized between invocations of the interpreter so you would not get the same hash number for the same input string when you restart your application:

    def hash_range(obj, min, max)
      (obj.hash % (max-min).abs) + [min, max].min
    end
    
    hash_range(did, min_cid, max_cid) # => 3886226395
    

    And, of course, you’ll have to decide what to do about collisions. You’ll likely have to persist a bucket of input strings which map to the same value and decide how to resolve the conflicts if you are looking up by the mapped value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code, that should remove the access of users from a
I have a piece of code that should get the names of food items
I frequently have some code that should be run either on a schedule or
I have a small code block that should append text to the beg of
So far I have managed to write some code that should print the source
I have the following code that seems like it should set my insertParameter but
I have some debugging code that if executed while running with GBD attached should
I have this program that should execute a piece of code base on the
I have got a piece of code, that should countdown some number (in this
I have heard people state that Code Generators and T4 templates should not be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.