I am trying to generate random bitcoin Private_Keys in HEX format (32bit) but I want the randomness to have a value within a specific HEX range, say between




The following piece of code in Python 3 is a start but it simply generates random keys for the curve's entire field which is up to


This is the code:

    return binascii.hexlify(os.urandom(32)).decode('utf-8').upper()

How to specify the desired search range expanding on the above code?


  • Do you need to use Python for that? Because vanitygen-plus, an unofficial version of vanitygen can be used for that easily. – MCCCS Feb 5 '19 at 16:51
  • Yes, I am looking for suggestions for Python 3 based on the code from the question. – RobertH Feb 5 '19 at 18:08
  • may be something like this (pseudo code): const(32) + rand(8) – Denis Leonov Feb 5 '19 at 18:35

Use random.
Using your low and high values:

import random
def generate_private_key():
    low  = 0x18909BDE11F67C97A53C62F45E632EAB58EA0D73A5FAC9EB50295FAD40A57EB5
    high = 0xDD10559E1285B3EE0303B159B8D6D8D0B88E6168D1C1D6000000000000000000
    return str( hex( random.randrange( low, high ) ) )

And then just:


Note that you'll probably need to convert this hex address to wif, or extract a public key or bitcoin public address from it... If you need more easy Python code for doing this, you can check these little pieces of code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.