Shuffle lines

Randomly reorder lines (Fisher–Yates) for local lists and draws.

Overview

Shuffling data at random is harder than it looks. The most naive algorithm — generating a random number for each element and sorting by that value — produces biased distributions where certain positions appear more often than they should. The Fisher-Yates shuffle, formalized by Ronald Fisher and Frank Yates in 1938 and popularized in its computer form by Richard Durstenfeld in 1964, solves this elegantly: it walks the list from back to front and swaps each element with one chosen at random from those not yet processed. The result is a truly uniform permutation in O(n).

The quality of randomness depends on the generator used. Math.random() in modern JavaScript engines uses algorithms like xorshift128+ and produces numbers unpredictable enough for common use cases: shuffling flashcards, randomizing question order, creating samples for manual tests. For draws with legal or audit requirements — raffles, sports competitions, governance decisions — the right source is the Web Crypto API: window.crypto.getRandomValues() uses operating system entropy and is cryptographically strong.

There is an interesting paradox in human perception of randomness. When Spotify launched truly random shuffle for playlists, users complained it was broken — because songs from the same artist appeared back to back frequently, which is statistically expected in a uniform distribution. To feel more random to people, Spotify had to make the shuffle deliberately less random, ensuring greater spacing between artists. Human perception of chance is deeply non-uniform.

Technical deep dive

Practical applications of shuffling

  • Flashcards and studying: shuffling a list of words, formulas, or questions before each study session prevents the brain from memorizing the sequence instead of the content.
  • Tests and quizzes: randomizing the order of questions and answer choices reduces cheating and ensures that different versions of the same test are equivalent in difficulty.
  • Playlists and recommendation systems: randomly selecting a subset of items from a larger list for display is a sampling-without-replacement operation, which Fisher-Yates executes efficiently.
  • Machine learning dataset splitting: shuffling data before dividing into training, validation, and test sets prevents ordering bias that distorts model metrics.
  • Draws and games: determining the order of players, cards, or prizes with a reliable randomness source, recording the seed for later auditing if necessary.

Math.random() versus window.crypto.getRandomValues()

  • Math.random() is sufficient for shuffling flashcards, randomizing display lists, and UI tests. It is fast and available in all JavaScript contexts.
  • window.crypto.getRandomValues() uses operating system entropy (hardware noise, interrupt timings) and is cryptographically suitable for draws with real value.
  • The practical difference: if an adversary can observe previous Math.random() outputs and knows the algorithm, they can in theory predict future values. getRandomValues() does not have this problem.
  • For Node.js use, the equivalent is crypto.getRandomValues() or crypto.randomBytes(). For Python, use random.shuffle() for simple cases and the secrets module for security-sensitive applications.

Tool guide

  • What you are working with A text list, one entry per line.

  • What the tool does Randomly reorders lines (Fisher–Yates-style shuffle with browser randomness).

  • Why use it Informal draws, random order for manual A/B ideas, break bias from always the same list order.

Code Snippets

Fisher-Yates shuffle in JavaScript
function shuffleLines(text) {
  const lines = text.split('\n');
  // Fisher-Yates: walk from back to front
  for (let i = lines.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [lines[i], lines[j]] = [lines[j], lines[i]];
  }
  return lines.join('\n');
}
Cryptographically secure version
function shuffleSecure(text) {
  const lines = text.split('\n');
  const arr = new Uint32Array(lines.length);
  crypto.getRandomValues(arr);
  return lines
    .map((line, i) => ({ line, rand: arr[i] }))
    .sort((a, b) => a.rand - b.rand)
    .map(item => item.line)
    .join('\n');
}

Use

alfa
beta
gamma → ordem aleatória a cada embaralhada

FAQ

What is this tool for?

It runs fully in your browser: useful to validate, format, or convert data in everyday development.

Are my inputs sent to a server?

Processing happens locally with JavaScript. We do not store what you paste into the text areas.

Can I use this for real production data?

Use at your own risk. For secrets (passwords, tokens), prefer controlled environments and your company policies. And always review the generated contents. Never trust blindly things you see on the internet.