Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an array exArray with two types of integer elements: 1 and 2. I'd like to choose a position $k$ in the array with uniform random probability conditioned on it having an element with the value 1. Is there a one-liner to do this? Right now I'm using RandomInteger[{1,arrayLength}], which is a ridiculous way to proceed.

share|improve this question
up vote 1 down vote accepted
RandomChoice[Flatten[Position[{1, 2, 2, 2, 1, 1, 2, 2, 1, 2, 2, 2, 1}, 1]]]
share|improve this answer

Sjoerd already gave one good option, but depending on the circumstance a proceedural trial-and-error approach may be superior. If you want to generate a many random positions from a given list it would be better to find all "ones" positions and then use RandomSample and/or RandomChoice to extract what you want. However if you only want a limited number of random positions from a particular list it will be much faster to merely generate a random position, extract that element and check it, and start over if it is not the target type. Here is an example:

n = 5000000;
a = RandomInteger[{1, 2}, n];
RandomChoice[Join @@ Position[a, 1]] // Timing
{0.983, 4227176}

So it takes almost a second (on my machine) to pick a random "one" from the five million element list a. However we can generate 50,000 random "ones" positions in a fraction of the time with trial-and-error:

  For[p = 0, a[[p]] =!= 1, p = RandomInteger[{1, n}]]; p,
] // Timing // First

So a single random position takes about 3.12*10^-6 seconds. This assumes a uniform distribution of ones and non-ones, and a smaller percent of "target" values will result in longer average times, but I hope this illustrates the potential performance of this method.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.