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 9203525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:29:33+00:00 2026-06-17T23:29:33+00:00

I am generating a unique and random alphanumeric string segment to represent certain links

  • 0

I am generating a unique and random alphanumeric string segment to represent certain links that will be generated by the users. For doing that I was approaching with “uuid” number to ensure it’s uniqueness and randomness, but, as per my requirements the string shouldn’t be more than 5 characters long. So I dropped that idea.

Then I decided to generate such a string using random function of ruby and current time stamp.
The code for my random string goes like this:-

 temp=DateTime.now
 temp=temp + rand(DateTime.now.to_i)
 temp= hash.abs.to_s(36)   

What I did is that I stored the current DateTime in a temp variable and then I generated a random number passing the current datetime as parameter. Then in the second line actually added current datetime and random number together to make a unique and random string.

Soon I found,while I was testing my application in two different machines and send the request at the same time, it generated the same string(Though it’s rare) once after more than 100 trials.

Now I’m thinking that I should add one more parameter like mac address or client ip address before passing to_s(36) on temp variable. But can’t figure out how to do it and even then whether it will be unique or nor…

Thanks….

  • 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-06-17T23:29:34+00:00Added an answer on June 17, 2026 at 11:29 pm

    SecureRandom in ruby uses process id (if available) and current time. You can use the urlsafe_base64(n= 16) class method to generate the sequence you need. According to your requirements I think this is your best bet.

    Edit: After a bit of testing, I still think that this approach will generate non-unique keys. The way I solved this problem for barcode generation was:

    barcode= barcode_sql_id_hash("#{sql_id}#{keyword}")
    

    Here, your keyword can be time + pid.

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

Sidebar

Related Questions

Possible Duplicate: Generating Unique Random Numbers in Java Code will generate 6 random numbers
Possible Duplicate: Creating a unique alphanumeric 10-character string I need to generate unique random
I have to generate a unique and random string which is to be stored
I need to generate a unique string that is 30 characters in length. At
After a few days researching on hashing, generating random numbers or unique keys, I
I'm so fedup with NSDate string object. currently I am generating an unique id
I'm generating a random 10 character string with php, and inserting it into a
SchemaTool is generating unique index for associations that are OneToOne. I believe this is
Duplicate: Unique random numbers in O(1)? I want an pseudo random number generator that
I was facing this unique problem of generating a bit-mask based on the input

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.