# randomized algorithm

Published articles for randomized algorithm.

This is one page of public article previews, not the complete archive. Follow Next page to continue. Summaries are not the original full articles.

## Deterministic Primality Testing for Limited Bit Width

DevFeed: [Deterministic Primality Testing for Limited Bit Width](<https://devfeed.tech/articles/deterministic-primality-testing-for-limited-bit-width-40494.md>)

Original publisher: [Read original article](<https://www.jeremykun.com/2026/04/07/deterministic-miller-rabin/>)

Published: 2026-04-07T13:00:00Z

Content type: tutorial

Language: en

Sources: [Jeremy Kun](<https://devfeed.tech/sources/jeremy-kun.md>)

Topics: [Algorithms](<https://devfeed.tech/topics/algorithms.md>), [C++](<https://devfeed.tech/topics/c-plus-plus.md>), [Mathematics](<https://devfeed.tech/topics/mathematics.md>), [Cryptography](<https://devfeed.tech/topics/cryptography.md>), [homomorphic encryption](<https://devfeed.tech/topics/homomorphic-encryption.md>)

Tags: [algorithm](<https://devfeed.tech/tags/algorithm.md>), [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [code](<https://devfeed.tech/tags/code.md>), [cryptography](<https://devfeed.tech/tags/cryptography.md>), [homomorphic-encryption](<https://devfeed.tech/tags/homomorphic-encryption.md>), [mathematics](<https://devfeed.tech/tags/mathematics.md>), [miller-rabin](<https://devfeed.tech/tags/miller-rabin.md>), [oeis](<https://devfeed.tech/tags/oeis.md>), [primes](<https://devfeed.tech/tags/primes.md>), [programming](<https://devfeed.tech/tags/programming.md>), [randomized-algorithm](<https://devfeed.tech/tags/randomized-algorithm.md>)

### AI overview

This article explains how to perform deterministic Miller-Rabin primality testing for 32-bit integers. It presents C++ code using the bases 2, 3, 5, and 7, which the article states is deterministic for all 32-bit inputs, and discusses strong pseudoprimes and related research.

### Source excerpt

Problem: Determine if a 32-bit number is prime (deterministically) Solution: (in C++) // Bases to test. Using the first 4 prime bases makes the test deterministic // for all 32-bit integers. See https://oeis.org/A014233. int64_t bases[] = {2, 3, 5, 7}; inline int countTrailingZeros(uint64_t n) { if (n == 0) return 64; return __builtin_ctzll(n); } int64_t modularExponentiation(int64_t base, int64_t exponent, int64_t modulus) { int64_t res = 1; int64_t b = base % modulus; int64_t e = exponent; while (e > 0) { if (e & 1) { // Doesn't overflow because we assume 32-bit integer inputs res = (res * b) % modulus; } b = (b * b) % modulus; e >>= 1; } return res; } bool isPrime(int64_t n) { if (n < 2) return false; if (n < 4) return true; if (!

## "Practical Math" Preview: Collect Sensitive Survey Responses Privately

DevFeed: ["Practical Math" Preview: Collect Sensitive Survey Responses Privately](<https://devfeed.tech/articles/practical-math-preview-collect-sensitive-survey-responses-privately-40454.md>)

Original publisher: [Read original article](<https://www.jeremykun.com/2022/05/14/practical-math-preview-collect-sensitive-survey-responses-privately/>)

Published: 2022-05-14T09:40:49Z

Content type: tutorial

Language: en

Sources: [Jeremy Kun](<https://devfeed.tech/sources/jeremy-kun.md>)

Topics: [math](<https://devfeed.tech/topics/math.md>), [Statistics](<https://devfeed.tech/topics/statistics.md>), [Code](<https://devfeed.tech/topics/code.md>), [Software](<https://devfeed.tech/topics/software.md>)

Tags: [bias](<https://devfeed.tech/tags/bias.md>), [code](<https://devfeed.tech/tags/code.md>), [collect](<https://devfeed.tech/tags/collect.md>), [differential-privacy](<https://devfeed.tech/tags/differential-privacy.md>), [mathematics](<https://devfeed.tech/tags/mathematics.md>), [politics](<https://devfeed.tech/tags/politics.md>), [practical](<https://devfeed.tech/tags/practical.md>), [practical-math](<https://devfeed.tech/tags/practical-math.md>), [privacy](<https://devfeed.tech/tags/privacy.md>), [programming](<https://devfeed.tech/tags/programming.md>), [python](<https://devfeed.tech/tags/python.md>), [random](<https://devfeed.tech/tags/random.md>), [randomized-algorithm](<https://devfeed.tech/tags/randomized-algorithm.md>), [research](<https://devfeed.tech/tags/research.md>), [responses](<https://devfeed.tech/tags/responses.md>), [survey](<https://devfeed.tech/tags/survey.md>)

### AI overview

This chapter preview explains randomized response, a survey technique for estimating aggregate statistics about sensitive questions while preserving respondents' privacy. Respondents use private coin flips to introduce randomization and maintain plausible deniability.

### Source excerpt

This is a draft of a chapter from my in-progress book, Practical Math for Programmers: A Tour of Mathematics in Production Software. Tip: Determine an aggregate statistic about a sensitive question, when survey respondents do not trust that their responses will be kept secret. Solution: import random def respond_privately(true_answer: bool) -> bool: '''Respond to a survey with plausible deniability about your answer.''' be_honest = random.random() < 0.5 random_answer = random.random() < 0.

## Community Detection in Graphs -- a Casual Tour

DevFeed: [Community Detection in Graphs -- a Casual Tour](<https://devfeed.tech/articles/community-detection-in-graphs-a-casual-tour-40357.md>)

Original publisher: [Read original article](<https://www.jeremykun.com/2014/05/19/community-detection-in-graphs-a-casual-tour/>)

Published: 2014-05-19T10:00:32Z

Content type: article

Language: en

Sources: [Jeremy Kun](<https://devfeed.tech/sources/jeremy-kun.md>)

Topics: [Graphs](<https://devfeed.tech/topics/graphs.md>), [Mathematics](<https://devfeed.tech/topics/mathematics.md>), [Algorithms](<https://devfeed.tech/topics/algorithms.md>), [Algorithms, Complexity](<https://devfeed.tech/topics/algorithms-complexity.md>)

Tags: [approximation](<https://devfeed.tech/tags/approximation.md>), [approximation-algorithms](<https://devfeed.tech/tags/approximation-algorithms.md>), [cliques](<https://devfeed.tech/tags/cliques.md>), [clustering](<https://devfeed.tech/tags/clustering.md>), [community-detection](<https://devfeed.tech/tags/community-detection.md>), [erdos-renyi](<https://devfeed.tech/tags/erdos-renyi.md>), [graph](<https://devfeed.tech/tags/graph.md>), [graphs](<https://devfeed.tech/tags/graphs.md>), [mathematics](<https://devfeed.tech/tags/mathematics.md>), [modularity](<https://devfeed.tech/tags/modularity.md>), [network](<https://devfeed.tech/tags/network.md>), [newman](<https://devfeed.tech/tags/newman.md>), [np-hard](<https://devfeed.tech/tags/np-hard.md>), [power-law-distribution](<https://devfeed.tech/tags/power-law-distribution.md>), [random-graph](<https://devfeed.tech/tags/random-graph.md>), [randomized-algorithm](<https://devfeed.tech/tags/randomized-algorithm.md>), [technical](<https://devfeed.tech/tags/technical.md>), [walktrap](<https://devfeed.tech/tags/walktrap.md>)

### AI overview

This introductory article examines community detection in graphs. It explains the informal idea of a community, why defining one precisely and usefully is difficult, and how the clique-based approach leads to computationally intractable problems, including the NP-hardness of finding the largest clique.

### Source excerpt

Graphs are among the most interesting and useful objects in mathematics. Any situation or idea that can be described by objects with connections is a graph, and one of the most prominent examples of a real-world graph that one can come up with is a social network. Recall, if you aren't already familiar with this blog's gentle introduction to graphs, that a graph $ G$ is defined by a set of vertices $ V$, and a set of edges $ E$, each of which connects two vertices.

## Programming with Finite Fields

DevFeed: [Programming with Finite Fields](<https://devfeed.tech/articles/programming-with-finite-fields-40350.md>)

Original publisher: [Read original article](<https://www.jeremykun.com/2014/03/13/programming-with-finite-fields/>)

Published: 2014-03-13T10:00:11Z

Content type: tutorial

Language: en

Sources: [Jeremy Kun](<https://devfeed.tech/sources/jeremy-kun.md>)

Topics: [Programming](<https://devfeed.tech/topics/programming.md>), [math](<https://devfeed.tech/topics/math.md>), [Python](<https://devfeed.tech/topics/python.md>), [Code](<https://devfeed.tech/topics/code.md>), [Programming language](<https://devfeed.tech/topics/programming-language.md>)

Tags: [algorithms](<https://devfeed.tech/tags/algorithms.md>), [classes](<https://devfeed.tech/tags/classes.md>), [code](<https://devfeed.tech/tags/code.md>), [decorators](<https://devfeed.tech/tags/decorators.md>), [division-algorithm](<https://devfeed.tech/tags/division-algorithm.md>), [elliptic-curves](<https://devfeed.tech/tags/elliptic-curves.md>), [euclidean-algorithm](<https://devfeed.tech/tags/euclidean-algorithm.md>), [euclidean-domain](<https://devfeed.tech/tags/euclidean-domain.md>), [factoring](<https://devfeed.tech/tags/factoring.md>), [field-characteristic](<https://devfeed.tech/tags/field-characteristic.md>), [finite-fields](<https://devfeed.tech/tags/finite-fields.md>), [gcd](<https://devfeed.tech/tags/gcd.md>), [math](<https://devfeed.tech/tags/math.md>), [operator-overloading](<https://devfeed.tech/tags/operator-overloading.md>), [polynomial-ring](<https://devfeed.tech/tags/polynomial-ring.md>), [programming](<https://devfeed.tech/tags/programming.md>), [programming-language](<https://devfeed.tech/tags/programming-language.md>), [python](<https://devfeed.tech/tags/python.md>), [randomized-algorithm](<https://devfeed.tech/tags/randomized-algorithm.md>), [typecasting](<https://devfeed.tech/tags/typecasting.md>)

### AI overview

This tutorial explains how to implement number types in Python for arithmetic over finite fields. It introduces integers modulo a prime as a finite field and lays groundwork for later elliptic-curve arithmetic.

### Source excerpt

Back when I was first exposed to programming language design, I decided it would be really cool if there were a language that let you define your own number types and then do all your programming within those number types. And since I get excited about math, I think of really exotic number types (Boolean rings, Gaussian integers, Octonions, oh my!). I imagined it would be a language feature, so I could do something like this:

## Martingales and the Optional Stopping Theorem

DevFeed: [Martingales and the Optional Stopping Theorem](<https://devfeed.tech/articles/martingales-and-the-optional-stopping-theorem-40349.md>)

Original publisher: [Read original article](<https://www.jeremykun.com/2014/03/03/martingales-and-the-optional-stopping-theorem/>)

Published: 2014-03-03T10:00:38Z

Content type: tutorial

Language: en

Sources: [Jeremy Kun](<https://devfeed.tech/sources/jeremy-kun.md>)

Topics: [math](<https://devfeed.tech/topics/math.md>)

Tags: [2-sat](<https://devfeed.tech/tags/2-sat.md>), [conditional-probability](<https://devfeed.tech/tags/conditional-probability.md>), [expectation](<https://devfeed.tech/tags/expectation.md>), [gambling](<https://devfeed.tech/tags/gambling.md>), [martingales](<https://devfeed.tech/tags/martingales.md>), [mathematics](<https://devfeed.tech/tags/mathematics.md>), [optional-stopping-theorem](<https://devfeed.tech/tags/optional-stopping-theorem.md>), [primer](<https://devfeed.tech/tags/primer.md>), [probability-theory](<https://devfeed.tech/tags/probability-theory.md>), [random](<https://devfeed.tech/tags/random.md>), [random-variables](<https://devfeed.tech/tags/random-variables.md>), [randomized-algorithm](<https://devfeed.tech/tags/randomized-algorithm.md>), [stochastic-processes](<https://devfeed.tech/tags/stochastic-processes.md>), [theory](<https://devfeed.tech/tags/theory.md>)

### AI overview

This primer introduces martingales as models of fair betting games and explains their connection to probability theory. It begins with a geometric-distribution exercise involving repeated die throws, then introduces the ABRACADABRA problem using a monkey typing random letters.

### Source excerpt

This is a guest post by my colleague Adam Lelkes. The goal of this primer is to introduce an important and beautiful tool from probability theory, a model of fair betting games called martingales. In this post I will assume that the reader is familiar with the basics of probability theory. For those that need to refresh their knowledge, Jeremy's excellent primers (1, 2) are a good place to start.

## Optimism in the Face of Uncertainty: the UCB1 Algorithm

DevFeed: [Optimism in the Face of Uncertainty: the UCB1 Algorithm](<https://devfeed.tech/articles/optimism-in-the-face-of-uncertainty-the-ucb1-algorithm-40332.md>)

Original publisher: [Read original article](<https://www.jeremykun.com/2013/10/28/optimism-in-the-face-of-uncertainty-the-ucb1-algorithm/>)

Published: 2013-10-28T11:42:55Z

Content type: tutorial

Language: en

Sources: [Jeremy Kun](<https://devfeed.tech/sources/jeremy-kun.md>)

Topics: [Algorithm](<https://devfeed.tech/topics/algorithm.md>), [Mathematics](<https://devfeed.tech/topics/mathematics.md>), [Data Science](<https://devfeed.tech/topics/data-science.md>)

Tags: [algorithm](<https://devfeed.tech/tags/algorithm.md>), [bandit-learning](<https://devfeed.tech/tags/bandit-learning.md>), [bandits](<https://devfeed.tech/tags/bandits.md>), [big-o-notation](<https://devfeed.tech/tags/big-o-notation.md>), [calculus](<https://devfeed.tech/tags/calculus.md>), [confidence-bounds](<https://devfeed.tech/tags/confidence-bounds.md>), [exploitation](<https://devfeed.tech/tags/exploitation.md>), [exploration](<https://devfeed.tech/tags/exploration.md>), [machine-learning](<https://devfeed.tech/tags/machine-learning.md>), [mathematics](<https://devfeed.tech/tags/mathematics.md>), [programming](<https://devfeed.tech/tags/programming.md>), [python](<https://devfeed.tech/tags/python.md>), [random-variables](<https://devfeed.tech/tags/random-variables.md>), [randomized-algorithm](<https://devfeed.tech/tags/randomized-algorithm.md>), [science](<https://devfeed.tech/tags/science.md>)

### AI overview

This tutorial introduces the multi-armed bandit problem as a model of the exploration-exploitation tradeoff and presents the UCB1 algorithm, which the article describes as performing close to optimally.

### Source excerpt

startups The software world is always atwitter with predictions on the next big piece of technology. And a lot of chatter focuses on what venture capitalists express interest in. As an investor, how do you pick a good company to invest in? Do you notice quirky names like "Kaggle" and "Meebo," require deep technical abilities, or value a charismatic sales pitch? When it comes to innovation in software engineering and computer science, and that as a society we should value big pushes forward much more than we do.

## Reservoir Sampling

DevFeed: [Reservoir Sampling](<https://devfeed.tech/articles/reservoir-sampling-40325.md>)

Original publisher: [Read original article](<https://www.jeremykun.com/2013/07/05/reservoir-sampling/>)

Published: 2013-07-05T10:00:49Z

Content type: tutorial

Language: en

Sources: [Jeremy Kun](<https://devfeed.tech/sources/jeremy-kun.md>)

Topics: [Algorithm](<https://devfeed.tech/topics/algorithm.md>), [Python](<https://devfeed.tech/topics/python.md>), [data](<https://devfeed.tech/topics/data.md>), [datasets](<https://devfeed.tech/topics/datasets.md>)

Tags: [algorithm](<https://devfeed.tech/tags/algorithm.md>), [big-data](<https://devfeed.tech/tags/big-data.md>), [generator](<https://devfeed.tech/tags/generator.md>), [induction](<https://devfeed.tech/tags/induction.md>), [programming](<https://devfeed.tech/tags/programming.md>), [python](<https://devfeed.tech/tags/python.md>), [random](<https://devfeed.tech/tags/random.md>), [randomized-algorithm](<https://devfeed.tech/tags/randomized-algorithm.md>), [reservoir-sampling](<https://devfeed.tech/tags/reservoir-sampling.md>), [sample](<https://devfeed.tech/tags/sample.md>), [streaming-algorithms](<https://devfeed.tech/tags/streaming-algorithms.md>), [streaming-data](<https://devfeed.tech/tags/streaming-data.md>), [streams](<https://devfeed.tech/tags/streams.md>)

### AI overview

This tutorial explains reservoir sampling for selecting an item uniformly at random from a data stream whose size is unknown or too large to store in memory. Its Python algorithm keeps one item and replaces it at step k with probability 1/k, with an induction-based proof of uniform selection.

### Source excerpt

Problem: Given a data stream of unknown size $ n$, pick an entry uniformly at random. That is, each entry has a $ 1/n$ chance of being chosen. Solution: (in Python) import random def reservoirSample(stream): for k,x in enumerate(stream, start=1): if random.random() < 1.0 / k: chosen = x return chosen Discussion: This is one of many techniques used to solve a problem called reservoir sampling. We often encounter data sets that we'd like to sample elements from at random.

## Miller-Rabin Primality Test

DevFeed: [Miller-Rabin Primality Test](<https://devfeed.tech/articles/miller-rabin-primality-test-40323.md>)

Original publisher: [Read original article](<https://www.jeremykun.com/2013/06/16/miller-rabin-primality-test/>)

Published: 2013-06-16T18:55:40Z

Content type: tutorial

Language: en

Sources: [Jeremy Kun](<https://devfeed.tech/sources/jeremy-kun.md>)

Topics: [Python](<https://devfeed.tech/topics/python.md>), [Algorithms](<https://devfeed.tech/topics/algorithms.md>), [Algorithms, Complexity](<https://devfeed.tech/topics/algorithms-complexity.md>), [Cryptography](<https://devfeed.tech/topics/cryptography.md>)

Tags: [algorithm](<https://devfeed.tech/tags/algorithm.md>), [algorithms](<https://devfeed.tech/tags/algorithms.md>), [complexity-theory](<https://devfeed.tech/tags/complexity-theory.md>), [computational-complexity](<https://devfeed.tech/tags/computational-complexity.md>), [cryptography](<https://devfeed.tech/tags/cryptography.md>), [miller-rabin](<https://devfeed.tech/tags/miller-rabin.md>), [primes](<https://devfeed.tech/tags/primes.md>), [probabilistic](<https://devfeed.tech/tags/probabilistic.md>), [programming](<https://devfeed.tech/tags/programming.md>), [python](<https://devfeed.tech/tags/python.md>), [random-number-generators](<https://devfeed.tech/tags/random-number-generators.md>), [randomized-algorithm](<https://devfeed.tech/tags/randomized-algorithm.md>), [rsa](<https://devfeed.tech/tags/rsa.md>)

### AI overview

This tutorial explains the Miller-Rabin primality test, including its probabilistic error bound, Python implementation, and role in testing whether numbers are prime. It also discusses the algorithm's relevance to cryptography and complexity theory.

### Source excerpt

Problem: Determine if a number is prime, with an acceptably small error rate. Solution: (in Python) import random def decompose(n): exponentOfTwo = 0 while n % 2 == 0: n = n/2 exponentOfTwo += 1 return exponentOfTwo, n def isWitness(possibleWitness, p, exponent, remainder): possibleWitness = pow(possibleWitness, remainder, p) if possibleWitness == 1 or possibleWitness == p - 1: return False for _ in range(exponent): possibleWitness = pow(possibleWitness, 2, p) if possibleWitness == p - 1: return False return True def probablyPrime(p, accuracy=100): if p == 2 or p == 3: return True if p < 2: return False exponent, remainder = decompose(p - 1) for _ in range(accuracy): possibleWitness = random.