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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:47:40+00:00 2026-05-11T22:47:40+00:00

I used to generate random string in the following way (now I’ve switched to

  • 0

I used to generate random string in the following way (now I’ve switched to this method).

key = '%016x' % random.getrandbits(128)

The key generated this way is most often a 32 character string, but once I’ve got 31 chars.

This is what I don’t get: why it’s 32 chars, not 16? Doesn’t one hex digit take one character to print?

So if I ask for %016x – shouldn’t one expect sixteen chars with possible leading zeroes?

Why string legth is not always the same?

Test case

import random
import collections
stats = collections.defaultdict(int)
for i in range(1000000):
    key = '%016x' % random.getrandbits(128)
    length = len(key)
    stats[length] += 1

for key in stats:
    print key, ' ', stats[key]

Prints:

32   937911
27   1
28   9
29   221
30   3735
31   58123
  • 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-11T22:47:41+00:00Added an answer on May 11, 2026 at 10:47 pm

    Yes, but the format you’re using doesn’t truncate — you generate 128 random bits, which require (usually) 32 hex digits to show, and the %016 means AT LEAST 16 hex digits, but doesn’t just throw away the extra ones you need to show all of that 128-bit number. Why not generate just 64 random bits if that’s what you actually need? Less work for the random generator AND no formatting problems.

    To satisfy your side curiosity, the length is occasionally 31 digits because 1 time in 16 the top 4 bits will all be 0; actually 1 time in 256 all the top 8 bits will be 0 so you’ll get only 30 digits, etc. You’ve only asked for 16 digits, so the formatting will give the least number that’s >= 16 and doesn’t require the truncation you have not asked for.

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

Sidebar

Related Questions

I have the following method (used to generate friendly error messages in unit tests):
I have a little script that is used to generate a random string of
I'm using the following code to generate a random string: <?php function random_string( )
I've been using RandomStringUtils to generate random IDs to be used as database keys:
I'm using Math.random to generate a random string passed in an ajax call. The
I have used this code to encrypt an ID in C#. I now need
I have used this: Generate Google Analytics events (__utm.gif requests) serverside and this: http://www.garyrgilbert.com/blog/index.cfm/2008/10/21/Tracking-Digital-Content
I want to create a random string (token) which can be used to identify
I have the following code. The function is to generate a random ID of
I've been looking for a simple Java algorithm to generate a pseudo-random alpha-numeric string.

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.