Skip to content

RandomInteger ​

A uniform random integer, or a list (or array) of them -- Random over a Range, by its Wolfram name.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageRandomInteger!
RandomInteger()00 or 11, each with probability 1/21/2.
RandomInteger(max)uniform in {0,…,max}\{0, \ldots, max\}.
RandomInteger({min, max})uniform in {min,…,max}\{min, \ldots, max\}.
RandomInteger(range, n)a list of nn draws from range (either form above).
RandomInteger(range, {n1, …, nk})an n1×⋯×nkn_1 \times \cdots \times n_k array of draws.
Typed signatures
TypePackageApplies
((integer | list<integer>)?, (integer<0..> | list<integer<0..>>)?) random -> integer | listcombinatoricsalways

Domain: Collections

Details
  • Seeded, not free-running: call SeedRandom(seed) first for a reproducible sequence. Without it, a fixed default seed is used, so even a bare RandomInteger call is reproducible run to run — two fresh evaluations draw the same value.
  • The generator is our own (a small deterministic PRNG), not Wolfram's — the same seed draws a different sequence from Wolfram's. Only the shape and range of the answer are guaranteed to match.
  • A worked example that both seeds and draws needs the two calls in one expression; Last(List(SeedRandom(n), RandomInteger(…))) runs SeedRandom for its effect and keeps the draw.

Examples

Basic #

42 is the fixed default seed a bare RandomInteger call (no SeedRandom) also uses — reseeded explicitly here so the example stays reproducible regardless of whatever else has already drawn from this engine's shared stream (e.g. another head's own reference example, run earlier on the same worker — see RandomGraph)

live ↗
InLast([SeedRandom(42),RandomInteger(100)])\mathrm{Last}(\bigl\lbrack\mathrm{SeedRandom}(42), \mathrm{RandomInteger}(100)\bigr\rbrack)Out6060
Scope #

A {min, max} range, after seeding

live ↗
InLast([SeedRandom(1),RandomInteger([10,20])])\mathrm{Last}(\bigl\lbrack\mathrm{SeedRandom}(1), \mathrm{RandomInteger}(\bigl\lbrack10, 20\bigr\rbrack)\bigr\rbrack)Out1616

A count argument draws that many values

live ↗
InLength(Last([SeedRandom(2),RandomInteger(6,10)]))\mathrm{Length}(\mathrm{Last}(\bigl\lbrack\mathrm{SeedRandom}(2), \mathrm{RandomInteger}(6, 10)\bigr\rbrack))Out1010

A dimension list draws a nested array of that shape

live ↗
InShape(Last([SeedRandom(3),RandomInteger(1,[2,3])]))\mathrm{Shape}(\mathrm{Last}(\bigl\lbrack\mathrm{SeedRandom}(3), \mathrm{RandomInteger}(1, \bigl\lbrack2, 3\bigr\rbrack)\bigr\rbrack))Out(2,3)(2,3)
About
Examples4
Checked againstWolfram Language 2 agree2 differ
CrosswalkWolfram Language

See also: SeedRandom