2
$\begingroup$

Sorry if I didn't make it clear in the title but is there a way to construct a list from

montecarlo = If[Norm[RandomReal[{-1, 1}, {2, 1}]] < 1, 1, 0]

for an n number of times, like:

{0,1,1,0,1,0,1,0}
$\endgroup$
4
$\begingroup$

If you define your montecarlo using SetDelayed (:=) instead of Set (=) you can simply use Table[montecarlo, n] to generate a list n random numbers:

ClearAll[montecarlo]
montecarlo := If[Norm[RandomReal[{-1, 1}, {2, 1}]] < 1, 1, 0]

n = 10;
SeedRandom[1]
Table[montecarlo, n]

{0, 1, 0, 1, 1, 1, 1, 0, 1, 1}

A faster alternative is to generate a list of length n and to process it:

SeedRandom[1]
1 - UnitStep[Norm /@ RandomReal[{-1, 1}, {n, 2, 1}] - 1]

{0, 1, 0, 1, 1, 1, 1, 0, 1, 1}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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