---
title: Avoid hardcoded seeds in random number generators
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: >-
  Docs > Datadog Security > Code Security > Static Code Analysis (SAST) > SAST
  Rules > Avoid hardcoded seeds in random number generators
---

> For the complete documentation index, see [llms.txt](https://docs.datadoghq.com/llms.txt).

# Avoid hardcoded seeds in random number generators

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

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

{% /callout %}

## Metadata{% #metadata %}

**ID:** `dart-security/insecure-random-seed`

**Language:** Dart

**Severity:** Warning

**Category:** Security

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

## Description{% #description %}

Constructing `Random` with a hardcoded integer seed makes the entire sequence of generated values fully predictable and reproducible by anyone who reads the source code.

Never seed a PRNG with a constant. Use `Random()` for non-security contexts or `Random.secure()` for cryptographic purposes, both of which use unpredictable seed sources.

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

```dart
import 'dart:math';

void main() {
  final r1 = Random(0);
  final r2 = Random(42);
}
```

```dart
import 'dart:math';

void main() {
  final r = Random(0xFF);
}
```

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

```dart
import 'dart:math';

void main() {
  final r = Random();
  print(r.nextInt(100));
}
```

```dart
import 'dart:math';

void main() {
  final r = Random.secure();
  print(r.nextInt(100));
}
```

```dart
import 'dart:math';

void main(int seed) {
  final r = Random(seed);
  print(r.nextInt(100));
}
```
  Seamless integrations. Try Datadog Code SecurityDatadog Code Security 
{% icon name="icon-external-link" /%}
 