Search

Search papers and lessons

Random Shooting Method

Level 4 · 11 min read

Random shooting plans by drawing many random action sequences, simulating each through a model, and running the one with the lowest predicted cost, then re-planning at every step.

On this page

  • Colour = topic mix
  • Calculus
  • Probability
  • Linear algebra
  • Neural network basics
  • Discrete math
  • Inference
  • Sign in to see your progress

TL;DR

Random shooting is a way to pick the next action for a robot without solving an optimisation problem. It draws many random sequences of future actions, runs each one forward through a model of the system, adds up the predicted cost of each, and keeps the first action from the sequence with the lowest cost. The next step, it does the whole thing again from the new state.

It needs nothing from the model except the ability to simulate it, so it works with models that are not smooth, such as a neural network or a physics simulator. Its weakness is that random guesses become sparse quickly as the plan gets longer.

Where you'll see it: model-based reinforcement learning on legged and arm robots, where a learned dynamics model is too complicated to differentiate cheaply, and as a simple baseline for Model Predictive Control in research code.

Intuition

Throw darts, keep the best

Imagine you need to drive a cart to a target and you may choose any push over the next second. You cannot see the best push directly, so you try many. Pick ten thousand random push sequences, imagine each one playing out with a physics model, and score how close each one ends up to the target while using little effort. Then take the best-scoring sequence and do only its first push. Once the cart has moved, pick a fresh batch of random sequences from the new position and repeat.

The name comes from this shooting: each random sequence is a shot at the target, and the best shot wins. Nothing clever is going on in each shot. The cleverness is in the repetition. Because you re-plan after every step, a bad guess early on is corrected at the next step, and the plan is always built from where the robot actually is.

flowchart LR
  S[measure state x_t] --> R[draw K random action sequences]
  R --> P[simulate each through the model]
  P --> C[add up the cost of each rollout]
  C --> B[pick the lowest-cost sequence]
  B --> A[apply only its first action]
  A --> S

The loop is the same as Model Predictive Control. What changes is how the best plan is found. MPC usually solves a constrained optimisation with a gradient-based solver. Random shooting replaces that solver with sampling.

The math

Setup

Take a discrete-time system with state xtx_t and action utu_t. A model ff predicts the next state, xt+1=f(xt,ut)x_{t+1} = f(x_t, u_t). Over a horizon of HH steps, an action sequence is U=(u0,u1,…,uH−1)U = (u_0, u_1, \dots, u_{H-1}). Starting from the measured state x0x_0, the model rolls out a predicted state trajectory. We score the rollout with a running cost ℓ(x,u)\ell(x, u) at each step and a terminal cost ℓf(x)\ell_f(x) at the end of the plan:

J(U;x0)=∑k=0H−1ℓ(xk,uk)+ℓf(xH),xk+1=f(xk,uk).J(U; x_0) = \sum_{k=0}^{H-1} \ell(x_k, u_k) + \ell_f(x_H), \qquad x_{k+1} = f(x_k, u_k).

Here JJ is the predicted total cost of a plan, and xkx_k is the state the model predicts after kk steps. The running cost penalises distance from a goal and the size of the actions. The terminal cost scores where the plan ends. This is the same cost that Optimal Control and MPC use.

The algorithm

Random shooting approximates the minimisation over UU by sampling. At each time tt:

  1. Draw KK independent candidate sequences U(1),…,U(K)U^{(1)}, \dots, U^{(K)} from a proposal distribution p(U)p(U). A common choice is uniform on a box that respects the action limits.
  2. For each candidate, roll out the model from the measured state xtx_t and compute J(k)=J(U(k);xt)J^{(k)} = J(U^{(k)}; x_t).
  3. Select k∗=arg⁡min⁡kJ(k)k^* = \arg\min_k J^{(k)}.
  4. Apply the first action ut=U0(k∗)u_t = U^{(k^*)}_0 to the real system. Discard the rest of the plan.
  5. Measure the next state and repeat from step 1.

Because the plan is only a set of samples, the best predicted cost satisfies min⁡kJ(k)≥J⋆\min_k J^{(k)} \ge J^\star, where J⋆J^\star is the true minimum over all sequences. The gap closes as KK grows and the proposal covers the region where good plans live.

How many samples are enough

Suppose the good plans make up a fraction ε\varepsilon of the proposal's probability mass. The chance that a single draw is good is ε\varepsilon, so the chance that none of KK independent draws is good is (1−ε)K(1 - \varepsilon)^K. The chance that at least one is good is

P(at least one good draw)=1−(1−ε)K.P(\text{at least one good draw}) = 1 - (1 - \varepsilon)^K.

This is the central trade-off. For a fixed ε\varepsilon, the success probability rises towards 1 as KK grows, but slowly. For ε\varepsilon that is small, which happens when the horizon is long, you need very many samples. Each sample also needs a full rollout, so the cost of a plan is K⋅HK \cdot H model evaluations per control step.

5010015020025030035040000.20.40.60.81K, number of sampled sequencesP(at least one good sequence)
  • ε = 0.01
  • ε = 0.001
The chance of finding a good plan rises with the number of samples, and rises much more slowly when good plans are rare.

Worked examples

Example
Four samples on a one-dimensional cart

Use the same system as in the MPC lesson. The state moves by the action, xt+1=xt+utx_{t+1} = x_t + u_t. The running cost is ℓ(x,u)=x2+u2\ell(x, u) = x^2 + u^2, the terminal cost is ℓf(x)=x2\ell_f(x) = x^2, the horizon is H=2H = 2, and the measured state is x0=2x_0 = 2. Actions are limited to ∣u∣≤1.5|u| \le 1.5, so the unconstrained optimum is allowed. For a plan (u0,u1)(u_0, u_1), the rollout gives x1=2+u0x_1 = 2 + u_0 and x2=x1+u1x_2 = x_1 + u_1, and the cost is

J=(4+u02)+(x12+u12)+x22.J = (4 + u_0^2) + (x_1^2 + u_1^2) + x_2^2.

Suppose four candidate sequences were drawn. These are chosen by hand, to show the arithmetic.

  1. Candidate (0.5,−0.5)(0.5, -0.5): x1=2.5x_1 = 2.5, x2=2.0x_2 = 2.0. Cost =4.25+6.50+4.00=14.75= 4.25 + 6.50 + 4.00 = 14.75.
  2. Candidate (−1.0,−0.5)(-1.0, -0.5): x1=1.0x_1 = 1.0, x2=0.5x_2 = 0.5. Cost =5.00+1.25+0.25=6.50= 5.00 + 1.25 + 0.25 = 6.50.
  3. Candidate (−0.2,0.3)(-0.2, 0.3): x1=1.8x_1 = 1.8, x2=2.1x_2 = 2.1. Cost =4.04+3.33+4.41=11.78= 4.04 + 3.33 + 4.41 = 11.78.
  4. Candidate (−0.6,−0.2)(-0.6, -0.2): x1=1.4x_1 = 1.4, x2=1.2x_2 = 1.2. Cost =4.36+2.00+1.44=7.80= 4.36 + 2.00 + 1.44 = 7.80.
  5. The lowest cost is candidate 2, with 6.50. Apply its first action, u0=−1.0u_0 = -1.0. The state becomes x1=1.0x_1 = 1.0, and the controller samples again from there.
  6. The exact optimum for this problem is (−1.2,−0.4)(-1.2, -0.4), with cost 6.40, as the MPC lesson shows. None of the four draws hit it exactly, so the sampled answer is worse by 0.10.

Interpretation: four samples are enough to get a sensible first action, but not the best one. The error is small here because the cost surface is smooth and the good region is large. In a problem with more dimensions the good region shrinks, and the same four draws would miss it.

Example
How many samples a long horizon needs

Suppose a good plan is one whose total cost lies within a band that holds 1 percent of the proposal's probability mass, so ε=0.01\varepsilon = 0.01.

  1. With K=100K = 100 samples, the chance of at least one good plan is 1−0.99100=1−0.366=0.6341 - 0.99^{100} = 1 - 0.366 = 0.634.
  2. With K=300K = 300 samples, it is 1−0.99300=1−0.049=0.9511 - 0.99^{300} = 1 - 0.049 = 0.951.
  3. Tripling the samples takes the chance from about 63 percent to about 95 percent, so the cost is paid in model rollouts, roughly 3×3 \times as many per step.
  4. Now suppose the horizon is ten times longer, and the good fraction drops to ε=0.0001\varepsilon = 0.0001 because the plan has more freedom to go wrong. With K=300K = 300, the chance is 1−0.9999300=1−0.970=0.0301 - 0.9999^{300} = 1 - 0.970 = 0.030, about 3 percent.
  5. To recover a 95 percent chance at the smaller fraction, you need KK with 0.9999K=0.050.9999^{K} = 0.05, so K=ln⁡(0.05)/ln⁡(0.9999)≈29,960K = \ln(0.05)/\ln(0.9999) \approx 29{,}960 samples.

Interpretation: a fixed budget of samples works for short plans and fails for long ones. This is why random shooting is usually paired with a short horizon, a warm start from the previous plan, or a more focused proposal that concentrates samples where good plans are likely. The numbers above are analytic, not from an experiment.

Tradeoffs and failure modes

SituationWhat goes wrongBetter choice
Model is smooth and differentiableRandom shooting wastes samples that a gradient would find directlyGradient-based shooting or direct optimisation, such as iLQR or direct collocation
Long horizon with few samplesGood plans are almost never drawn, so the chosen action is poorShorter horizon, warm start from the last plan, or a refined proposal
Tight constraints that carve out a small feasible setUniform draws almost always violate a constraint and get discardedSample from a projected or learned proposal, or add penalty terms to the cost
Model is inaccurate far from the dataThe lowest predicted cost can belong to a plan that exploits model errorEnsembles of models, with disagreement added as a penalty, and short horizons
Real-time control on a robotThe sample count must fit the control periodParallel rollouts on a GPU, or fewer samples per step
Sample-to-sample noise mattersTwo runs can pick different plans, so behaviour looks jitteryFix the random seed for evaluation, or average the top few plans

Random shooting is the simplest member of a family. The cross-entropy method refits a Gaussian to the best samples and repeats, so the proposal moves towards good plans. MPPI weights each sample by an exponential of its cost, which gives a smooth average rather than a single winner. Both keep the same rollout-and-score core. Random shooting is easier to implement and reason about, and it is usually the right first baseline.

History

Choosing control inputs by simulating many candidate futures is much older than the name. Model predictive control was developed in the process industries from the 1970s, where a model of a plant was used to plan a few steps ahead at each sample time. The optimisation at each step was usually done with a numerical solver. Using random candidate sequences in place of a solver is a natural shortcut that appears throughout the stochastic-optimisation and robotics literature, where it is a standard baseline.

The method gained attention in learning-based robotics. In 2017, Nagabandi and colleagues trained neural network dynamics models for robots and used random shooting with those models to choose actions, then fine-tuned a policy on top. The paper showed that a simple sampling planner, combined with a learned model, could control real robots with few samples of experience. Its value came from how little it asked of the model. The planner needed only forward simulation, which a neural network provides cheaply, and no derivatives.

Check yourself

QuestionWhy does random shooting re-plan at every step, even though it computed a whole plan of H actions?Show answer

The model is an approximation, and the real system drifts from the prediction because of noise and model error. Only the first action is applied, and then the robot measures its actual state. Re-planning from that state corrects the drift and keeps the plan tied to reality. Using the whole predicted plan open loop would let errors compound over the horizon.

QuestionWith ε=0.05\varepsilon = 0.05 and K=60K = 60 samples, what is the probability that at least one sample is good?Show answer

The chance that no sample is good is (1−0.05)60=0.9560(1 - 0.05)^{60} = 0.95^{60}. Since ln⁡0.95≈−0.0513\ln 0.95 \approx -0.0513, this is e−3.08≈0.046e^{-3.08} \approx 0.046. So the probability that at least one sample is good is 1−0.046=0.9541 - 0.046 = 0.954, about 95 percent.

QuestionA planner with a learned neural network model needs gradients through the model, but the network is large and the cost is not differentiable at the goal. Is random shooting a good fit?Show answer

Random shooting needs only forward rollouts, so a non-differentiable cost is not a problem, and a large network is fine as long as each forward pass is fast enough for the sample budget. It is a reasonable fit for that setting. The weakness is that it needs many rollouts for long horizons, so a short horizon, a warm start, or parallel evaluation may be needed to meet the control period.

QuestionIn the four-candidate cart example, suppose the exact optimum (−1.2,−0.4)(-1.2, -0.4) had also been drawn. Which plan would random shooting apply, and what does this show?Show answer

That plan costs 6.40, which is lower than the 6.50 of the best drawn candidate, so random shooting would apply its first action, u0=−1.2u_0 = -1.2. The planner can only return a plan it has sampled, so the quality of the result depends on how many of the draws land near the good region. The more candidates that cover it, the closer the applied action is to the optimum.

Sources