Math Functions

Lesson 90: Using randomSeed() Function

Learn how randomSeed() sets pseudo-random starting state and improves variability in random() sequences.

Progress indicator

Lesson 90 of 90

Learning Objectives

  • Understand what randomSeed() does and how it affects pseudo-random sequences.
  • Use randomSeed() syntax correctly in setup() for repeatable or variable behavior.
  • Learn the pseudo-random concept and why seed values matter after reset.
  • Compare randomSeed() and random() roles clearly.
  • Avoid common seeding mistakes that reduce randomness quality.
  • Debug seed-related behavior by checking serial logs and reboot sequence patterns.

Concept Explanation

What is randomSeed()

randomSeed() sets the starting state (seed) for Arduino's pseudo-random generator.

It does not generate random numbers itself. It only decides where the random sequence starts.

Think of seed as the starting page of a number sequence. Same page, same sequence. Different page, different sequence.

randomSeed() Syntax

Basic syntax: randomSeed(seedValue)

  • randomSeed(1234): fixed sequence on every reset.
  • randomSeed(analogRead(34)): usually different sequence after reset.

In most projects, call randomSeed() once in setup().

How randomSeed() Works

  1. Take integer seed input.
  2. Initialize internal pseudo-random state with that seed.
  3. random() calls then produce a sequence based on that state.

Same seed + same call order equals same sequence. Different seed gives different sequence.

This is useful for both testing (fixed behavior) and demos (variable behavior).

Pseudo-Random Concept

Pseudo-random means algorithm-generated but random-looking. It is deterministic from seed.

Great for blinking patterns, simple games, and simulation effects. Not suitable for cryptographic security.

Seeding vs True Random

Seeding chooses the starting point of a deterministic generator. True random comes from unpredictable physical events.

Practical embedded pattern: seed from noise once, then use pseudo-random values quickly in loop execution.

randomSeed() vs random() (Comparison)

  • randomSeed(): initializes random engine state.
  • random(): returns next value from current engine state.

You usually call randomSeed() once in setup(), then random() repeatedly in loop().

When to Use randomSeed()

  • When you want different random behavior after each reset.
  • When demo patterns should not look repeated every boot.
  • When testing requires reproducible sequences using a fixed seed.
  • When game-like behavior needs controlled variation.

Real-life example: startup LED pattern can feel unique each reboot by seeding from analog noise instead of a fixed number.

Example Code

This example seeds random once, then generates changing blink delays in loop().

const int LED_PIN = 2;
int blinkDelay = 500;

void setup() {
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(115200);

  // Use analog noise to set a changing seed.
  int seedValue = analogRead(34);
  randomSeed(seedValue);

  Serial.print("Seed used: ");
  Serial.println(seedValue);
}

void loop() {
  blinkDelay = random(200, 1001); // 200..1000

  digitalWrite(LED_PIN, HIGH);
  delay(120);
  digitalWrite(LED_PIN, LOW);
  delay(blinkDelay);

  Serial.print("Next random delay(ms): ");
  Serial.println(blinkDelay);
}

Example Code Explanation

  1. Read analog noise once and store it in seedValue.
  2. Call randomSeed(seedValue) in setup() to set generator start state.
  3. Generate random delay values using random(200, 1001) in loop().
  4. Blink LED with fixed ON time and variable OFF time.
  5. Print seed and generated delays to Serial monitor for easy debugging.
  6. With changing seed, rebooting the board usually gives a different delay sequence.
  7. With fixed seed, rebooting gives the same sequence, useful for testing.

What Happens Inside

  1. CPU reads analog pin and gets seed input value.
  2. randomSeed() initializes pseudo-random internal state.
  3. Each random() call advances generator state.
  4. Generated value is bounded to requested range before returning.
  5. Loop uses returned value to create timing variation.
  6. Serial output shows exact runtime values for verification.
Internal flow model: startup seed input -> PRNG state initialization -> repeated random() state advancement -> bounded outputs -> application behavior changes.

Common Mistakes with randomSeed()

  • Calling randomSeed() repeatedly inside loop(), reducing useful variation.
  • Using a constant seed unintentionally and expecting new behavior each boot.
  • Confusing randomSeed() with random() and expecting output values directly.
  • Choosing a very stable seed source with little noise variation.
  • Not printing seed and outputs during debugging sessions.

Best Practices for randomSeed()

  • Seed once in setup() unless you intentionally need reseeding.
  • Use noisy analog sources for more variable startup sequences.
  • Use fixed seed for reproducible debugging and automated tests.
  • Log seed and sample outputs to validate runtime behavior quickly.
  • Combine good seeding with safe random(min, max) ranges for stable hardware behavior.

Try it now

Open the simulator workspace and observe seed-driven random delay sequences in the timeline and serial monitor.

Run in Simulator