Hot answers tagged

19

You can intersect the sphere and a cylinder, and then use RandomPoint. For example, here is a random point on the sphere: sphere = Sphere[]; SeedRandom[1] pt = RandomPoint[sphere] {0.707037, 0.595614, 0.381239} Then, you create a cylinder in he direction of the random point with a radius: r = .7; cylinder = Cylinder[{{0,0,0},pt}, r]; Now, intersect ...


11

Using Christian Blatter's results from this math.SE answer, here is how to randomly sample a spherical cap: randomCapPoint[{r_, r2_}, dir_?VectorQ] := With[{h = RandomReal[{Sqrt[1 - (r2/r)^2], 1}]}, RotationTransform[{{0, 0, 1}, Normalize[dir]}][r Append[Sqrt[1 - h^2] Normalize[RandomVariate[NormalDistribution[], 2]], h]]] For example, ...


8

We can also take RandomPoints in boolean region obtained by the RegionIntersection of a Sphere and a Ball with radius r centered at a random point on the sphere: SeedRandom[1] r = .7; ctr = RandomPoint[Sphere[]]; pts = RandomPoint[RegionIntersection[Ball[ctr, r], Sphere[]], 1000]; Graphics3D[{Red, Point@pts, White, Opacity[.5], Sphere[]}]


7

To localize r, I would use Module. Otherwise, the only changes needed are inserting a missing semicolon and getting rid of the unnecessary Return calls. ClearAll[randomHop]; randomHop[{x_,y_}]:=Module[{ r }, r=RandomInteger[{1,2}]; If[r==1, {{0.5,0.5},{0.5,0.5}}.{x,y}, {{-0.5,-0.5},{0.5,-0.5}}.{x,y}+{1,0} ] ]; SeedRandom[1]; ...


5

Just make a random matrix with all positive eigenvalues and then add a minus sign: r = 4; M = -#.ConjugateTranspose[#]&[RandomVariate[NormalDistribution[], {r,r,2}].{1,I}]


4

For this question, I can find some code of python version, Refs. here. And I rewrote this Ising simulation as Mathematica version, as follow, 1. Define a function used for generating a table (as configuration) Initialstate[n_Integer] := 2*Table[RandomInteger[], {n}, {n}] - 1 where n as the number of points. And define a function used for getting any point'...


3

ClearAll[step, iterate] step[initialvector_, indicesdecay_, rangedecay_, indicesgrow_, rangegrow_] := Module[{ca = Normal[ SparseArray[Join[Thread[indicesdecay -> -1], Thread[indicesgrow -> 1]], Length@initialvector]]}, initialvector (1 + (ca /. {1 :> RandomReal[rangegrow], -1 :> RandomReal[rangedecay]}))] iterate[...


3

The following will give you a long string of text using common words separated by a space; it is very fast after the first execution (which loads some indices): StringRiffle@RandomWord["CommonWords", 100] "dexterous calibration ethical nocturnal misfortune ruining commodious refreshing gable arithmetic sacristy doorknocker thread measles pittance ...


2

Starting as you do, let's write: SeedRandom[123]; list1 = RandomReal[{0.3, 0.8}, 4] which produces {0.52786, 0.788913, 0.771607, 0.781108} Next, we want to increase each of these a small amount. You could do this: smallRange = {0.05, 0.1}; list2 = list1 + RandomReal[smallRange, 4] My session produces {0.592977, 0.862248, 0.824689, 0.85039} Next, ...


1

randomHop[{x_, y_}] := If[RandomInteger[{1, 2}] == 1, {{0.5, 0.5}, {0.5, 0.5}}.{x, y}, {{-0.5, -0.5}, {0.5, -0.5}}.{x, y} + {1.0, 0}] also works.


1

Here's a few pieces of the puzzle. To generate monotonic sequences of "random" numbers: Accumulate[RandomReal[{0, 0.1}, 10] {0.0669849, 0.0935961, 0.141986, 0.216063, 0.238291, 0.255048, 0.269631, 0.276363, 0.284295, 0.329844} Now let's say you have a vector x and you want the first few to go up a tiny bit and the rest to go down. Specify the ...


1

Your probability distribution has a mean of 2. That means the expected value of 500 random variates will be 1000. Therefore, the following will work: SeedRandom[42] sample = With[{sum = 1000, d = 10}, Module[{testSum = 1, sample}, While[testSum < sum - d || testSum > sum + d, sample = Quiet @ RandomVariate[ ...


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