RandomChoice

RandomChoice[{e1,e2,}]

gives a pseudorandom choice of one of the ei.

RandomChoice[list,n]

gives a list of n pseudorandom choices.

RandomChoice[list,{n1,n2,}]

gives an n1×n2× array of pseudorandom choices.

RandomChoice[{w1,w2,}{e1,e2,}]

gives a pseudorandom choice weighted by the wi.

RandomChoice[wlistelist,n]

gives a list of n weighted choices.

RandomChoice[wlistelist,{n1,n2,}]

gives an n1×n2× array of weighted choices.

Details

  • RandomChoice[{e1,e2,}] chooses with equal probability between all of the ei.
  • RandomChoice gives a different sequence of pseudorandom choices whenever you run the Wolfram Language. You can start with a particular seed using SeedRandom.
  • A Method option to SeedRandom can be given to specify the pseudorandom generator used.

Examples

open allclose all

Basic Examples  (4)

Choose among , , and at random:

A list of 20 random choices:

A 4×3 array of random choices:

Choices weighted with probabilities:

Weights are automatically normalized:

Scope  (2)

The choices can be any expressions:

Choose among all numbers between -10 and 10:

Choose among even numbers between -10 and 10:

Applications  (9)

Generate a random string:

Generate a weighted random string:

Generate integers according to a distribution:

Random array of color choices:

A random walk with steps of unit length:

2D random walk:

Random walk on a hexagonal lattice:

Randomly generate disks or circles:

A random graph with 1% of possible edges filled in:

Properties & Relations  (2)

Use SeedRandom to get repeatable random values:

Use BlockRandom to block one use of RandomChoice from affecting others:

Neat Examples  (5)

Pick 10 random words beginning with "a" from the dictionary:

Pick 10 random Wolfram Language functions:

Make a continuous string of characters:

Break the characters into "words":

2D random walk with eight directions:

3D lattice random walk:

Introduced in 2007
 (6.0)