For a family of graphs
F
\mathcal {F}
, a graph
G
G
is
F
\mathcal {F}
-universal if
G
G
contains every graph in
F
\mathcal {F}
as a (not necessarily induced) subgraph. For the family of all graphs on
n
n
vertices and of maximum degree at most two,
H
(
n
,
2
)
\mathcal {H}(n,2)
, we prove that there exists a constant
C
C
such that for
p
≥
C
(
log
n
n
2
)
1
3
,
p \geq C \left ( \frac {\log n}{n^2} \right )^{\frac {1}{3}},
the binomial random graph
G
(
n
,
p
)
G(n,p)
is typically
H
(
n
,
2
)
\mathcal {H}(n,2)
-universal. This bound is optimal up to the constant factor as illustrated in the seminal work of Johansson, Kahn, and Vu for triangle factors. Our result improves significantly on the previous best bound of
p
≥
C
(
log
n
n
)
1
2
p \geq C \left (\frac {\log n}{n}\right )^{\frac {1}{2}}
due to Kim and Lee. In fact, we prove the stronger result that for
H
ℓ
(
n
,
2
)
\mathcal {H}^{\ell }(n,2)
, the family of all graphs on
n
n
vertices, of maximum degree at most two and of girth at least
ℓ
\ell
,
G
(
n
,
p
)
G(n,p)
is typically
H
ℓ
(
n
,
2
)
\mathcal H^{\ell }(n,2)
-universal when
p
≥
C
(
log
n
n
ℓ
−
1
)
1
ℓ
.
p \geq C \left (\frac {\log n}{n^{\ell -1}}\right )^{\frac {1}{\ell }}.
This result is also optimal up to the constant factor. Our results verify (in a weak form) a classical conjecture of Kahn and Kalai.