Hot answers tagged

9

Slightly expanding my comment. It is a partial answer, explaining most but not all of the observed timing increase. First I show some representative timings. Note that I use 2^16-1 and 2^21-1 below, for reasons that will be explained. AbsoluteTiming[RandomInteger[2^16 - 1, 100000000];] AbsoluteTiming[RandomInteger[2^21 - 1, 100000000];] (* Out[37]= ...


8

Here is an approach using RandomPoint and graphics primitives: pts = RandomPoint[Rectangle[], 10^6]; (* generate random points on the unit square *) rm = RegionMember[Disk[{0.5, 0.5}, 0.5]]; (* RegionMemberFunction for an embedded Disk *) Now we count the number of points that fall in the circle and divide that by the total number of points. That should ...


8

There is an example in the documentation which may get you started: pairs = RandomReal[{-1, 1}, {10000, 2}]; 4 Count[Map[Norm, pairs], _?(# <= 1 &)]/10000. (*3.1248*) You can plot see this as: Graphics[{PointSize[Small], Blue, Point@Select[pairs, Norm[#] <= 1 &], Gray, Point@Select[pairs, Norm[#] > 1 &], Red, Thick, Circle[]}, ...


3

Does this do what you want? stepTypes = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; {pos1, pos2} = RandomInteger[{0, 4}, {2, 2}]; While[pos1 != pos2, {pos1, pos2} = Mod[{pos1, pos2} + RandomChoice[stepTypes, 2], 5]; Print[pos1, pos2];]


2

Thanks for the solution, J.M. (* create an example pdf *) f[x_] := Exp[-x^4 + x^2]; normf = NIntegrate[f[x], {x, -Infinity, Infinity}]; p[x_] = f[x]/normf; Plot[p[x], {x, -2, 2}] (* How to generate samples from p[x] *) pd = ProbabilityDistribution[p[x], {x, -Infinity, Infinity}]; d = RandomVariate[pd, 100000]; Histogram[d, 100]


1

It may be marked as a duplicate, but the answer to that post is more complicated because of the constraint that the cylinders do not intersect. For this question, just create one cylinder with the right length and radius and rotate and translate it randomly, radius = .3; length = 5; cyl = Cylinder[{{0, 0, 0}, {0, 0, length}}, radius]; cylinder := cyl // ...


1

n = 5; r = 0.2; pt = RandomReal[{-1, 1}, {n, 2}] Graphics3D[Cylinder[{{#[[1]], #[[2]], 0}, {#[[1]], #[[2]], 1}}, r] & /@ pt] If you want more n = 5; r = 0.2; pt1 = RandomReal[{-1, 1}, {n, 3}]; pt2 = RandomReal[{-1, 1}, {n, 3}]; Graphics3D[Cylinder[{pt1[[#]], pt2[[#]]}, r] & /@ Range[n]]


1

I post this for fun but perhaps it may be useful. vm = VoronoiMesh[RandomReal[10, {20, 2}], MeshCellStyle -> {{1, All} -> Black, {2, All} -> LightYellow}]; pg = DiscretizeRegion /@ MeshPrimitives[vm, 2]; f[p_, r_] := With[{v = RandomVariate[PoissonDistribution[r Area[p]]]}, If[v == 0, {}, {Blue, Point[RandomPoint[p, v]], Red, ...



Only top voted, non community-wiki answers of a minimum length are eligible