I wonder if anybody can help, I need a hand with a simple simulation I am writing with Mathematica (I am using version 8). Basically it creates a list (called l) with numbers from 1 to n and a random number v in the same interval. Then it draws a random number s from l and checks if it is equal to v. If s is not equal to v, that number is dropped from l and another s is drawn and so on, the cycle stops when s=v. The number i is used to count the draws and the list k to show all the i's. Here is the code with n=5000 and 5 runs
n = 5000;
runs = 5;
(* initialization *)
k = ConstantArray[0, runs];
Table[
{v = RandomInteger[{1, n}];
s = 0;
i = 0;
l = Range[1, n];
While[s != v,
s = RandomChoice[l];
p = Flatten[Position[l, s]];
l = Drop[l, p];
i = i + 1;];
k[[j]] = i}
, {j, 1, runs}]
k
The problem is I would like to run thousands of simulations with n raging from 30000 to 50000, but this code is way too slow. Is there a way to speed it up?
p = Flatten[Position[l, s]]; l = Drop[l, p]tol=DeleteCases[l,s]. – b.gates.you.know.what Apr 05 '13 at 20:20