Skip to content

Arduino random() Returns Same Sequence Every Run

DodaTech Updated 2026-06-26 1 min read

In this tutorial, you'll learn about Arduino random() Returns Same Sequence Every Run. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Problem

random() produces identical sequences every time the board is reset.

Quick Fix

Wrong

int r = random(100);  // Same every reset
Same sequence of numbers every power cycle.
void setup() {
  Serial.begin(9600);
  // Seed from floating analog pin
  randomSeed(analogRead(A0));
}

void loop() {
  int r = random(100);
  Serial.println(r);
  delay(500);
}
Different sequence of random numbers each reset.

Prevention

Always call randomSeed() with a noise source. Do NOT use a fixed value like randomSeed(42). The best seed is analogRead() on a floating analog pin. For better randomness, XOR multiple analog readings. On ESP32, use the hardware random number generator (esp_random()).

DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.

FAQ

### Why does analogRead(A0) work as a seed?

A floating analog pin picks up ambient electrical noise, producing a near-random starting value.

Can I use millis() as a seed?

millis() starts from 0 on reset, so it produces the same initial value every time. Not useful alone. Combine with analogRead.

Is Arduino random() cryptographically secure?

No. It uses a pseudo-random number generator (PRNG). For security, use ESP32 hardware RNG or an external TRNG module.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro