---
title: use secrets package over random package
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: >-
  Docs > Datadog Security > Code Security > Static Code Analysis (SAST) > SAST
  Rules > use secrets package over random package
---

# use secrets package over random package

{% callout %}
# Important note for users on the following Datadog sites: app.ddog-gov.com

{% alert level="danger" %}
This product is not supported for your selected [Datadog site](https://docs.datadoghq.com/getting_started/site). ().
{% /alert %}

{% /callout %}

## Metadata{% #metadata %}

**ID:** `python-security/avoid-random`

**Language:** Python

**Severity:** Error

**Category:** Security

**CWE**: [330](https://cwe.mitre.org/data/definitions/330.html)

## Description{% #description %}

Make sure to use values that are *actually* random. The `random` module in Python should generally not be used and replaced with the `secrets` module, as noted in the [official Python documentation](https://docs.python.org/3/library/random.html).

#### Learn More{% #learn-more %}

- [CWE-330](https://cwe.mitre.org/data/definitions/330.html)
- [Python random module documentation](https://docs.python.org/3/library/random.html)
- [Python secrets module documentation](https://docs.python.org/3/library/secrets.html#module-secrets)

## Non-Compliant Code Examples{% #non-compliant-code-examples %}

```python
from random import randrange

randrange(10) # randrange is not actually random
```

```python
from random import random

v = random() # random is not actually random
```

```python
import random

n = random.randrange(10) # randrange is not actually random
```

```python
import random

n = random.random(1) # random is not actually random
```

```python
import random

n = random.random() # random is not actually random
```

## Compliant Code Examples{% #compliant-code-examples %}

```python
# The random package was not imported
n = random.random()
```
  Seamless integrations. Try Datadog Code SecurityDatadog Code Security 
{% icon name="icon-external-link" /%}
 