  The output randomness from a random number generator can be certified by
observing the violation of quantum contextuality inequalities based on the
Kochen-Specker theorem. Contextuality can be tested in a single quantum system,
which significantly simplifies the experimental requirements to observe the
violation comparing to the ones based on nonlocality tests. However, it is not
yet resolved how to ensure compatibilities for sequential measurements that is
required in contextuality tests. Here, we employ a modified
Klyachko-Can-Binicio\u{g}lu-Shumovsky contextuality inequality, which can ease
the strict compatibility requirement on measurements. On a trapped single \Ba
ion system, we experimentally demonstrate violation of the contextuality
inequality and realize self-testing quantum random number expansion by closing
detection loopholes. We perform $1.29 \times 10^8$ trials of experiments and
extract the randomness of $8.06 \times 10^5$ bits with a speed of 270 bits
s$^{-1}$. Our demonstration paves the way for the practical high-speed
spot-checking quantum random number expansion and other secure information
processing applications.
