<p>Use random.<br>
Using your low and high values:</p>

<pre><code>import random
def generate_private_key():
    low  = 0x18909BDE11F67C97A53C62F45E632EAB58EA0D73A5FAC9EB50295FAD40A57EB5
    high = 0xDD10559E1285B3EE0303B159B8D6D8D0B88E6168D1C1D6000000000000000000
    return str( hex( random.randrange( low, high ) ) )
</code></pre>

<p>And then just:</p>

<pre><code>print(generate_private_key())
</code></pre>

<p>Note that you'll probably need to convert this <a href="https://en.bitcoin.it/wiki/Wallet_import_format" rel="nofollow noreferrer">hex address to wif</a>, or extract a public key or bitcoin public address from it... If you need more easy Python code for doing this, you can check <a href="https://github.com/circulosmeos/bitcoin-in-tiny-pieces" rel="nofollow noreferrer">these little pieces of code</a>.</p>

CC BY-SA 4.0
2019-02-07T12:49:23.670Z
2019-02-07T12:57:08.480Z
2019-02-07T12:57:08.480Z
circulosmeos
<p>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 </p>

<pre><code>18909BDE11F67C97A53C62F45E632EAB58EA0D73A5FAC9EB50295FAD40A57EB5
</code></pre>

<p>and </p>

<pre><code>DD10559E1285B3EE0303B159B8D6D8D0B88E6168D1C1D6000000000000000000
</code></pre>

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

<p><code>FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141</code></p>

<p>This is the code:</p>

<pre><code>generate_private_key():
    return binascii.hexlify(os.urandom(32)).decode('utf-8').upper()
</code></pre>

<p>How to specify the desired search range expanding on the above code? </p>

<p>Thanks. </p>

CC BY-SA 4.0
2019-02-05T16:51:33.870Z
Do you need to use Python for that? Because vanitygen-plus, an unofficial version of vanitygen can be used for that easily.
MCCCS
CC BY-SA 4.0
2019-02-05T18:08:13.223Z
Yes, I am looking for suggestions for Python 3 based on the code from the question.
Robert
CC BY-SA 4.0
2019-02-05T18:35:26.880Z
may be something like this (pseudo code): const(32) + rand(8)
Denis Leonov
<private-key>
How to specify PrivateKey search range in HEX?
Robert
# How to specify PrivateKey search range in HEX?

(Asked by: username_0 on 2019-02-05)

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

```
18909BDE11F67C97A53C62F45E632EAB58EA0D73A5FAC9EB50295FAD40A57EB5

```

and

```
DD10559E1285B3EE0303B159B8D6D8D0B88E6168D1C1D6000000000000000000

```

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

`FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141`

This is the code:

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

```

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

Thanks.

**Tags:** private-key

> **Comments:**
> **username_1**: Do you need to use Python for that? Because vanitygen-plus, an unofficial version of vanitygen can be used for that easily.  
> **username_0**: Yes, I am looking for suggestions for Python 3 based on the code from the question.  
> **username_2**: may be something like this (pseudo code): const(32) + rand(8)  

---

## 1 answers

### Answer by username_3 on 2019-02-07. Score: 1 | Accepted answer

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:

```
print(generate_private_key())

```

Note that you'll probably need to convert this [hex address to wif](https://en.bitcoin.it/wiki/Wallet_import_format), 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](https://github.com/username_3/bitcoin-in-tiny-pieces).


---

