I'm trying to generate a spherical distribution of radial random walk points in 3D space. The following code works, but the random walk lines aren't radial. Why ? Where is my mistake ?

MinSprite := 0.006; (* min radius of sprites *)
MaxSprite := 0.03; (* max radius of sprites *)
SpriteOverlap := 0.75; (* min separation between sprites *)
IterationStep := 0.1;
NumberOfSteps := 20;
thickness = 0.09;
pointsmean = 20;
pointssd = 12;

SpriteSize[p_] := MinSprite + (MaxSprite - MinSprite)Norm[p];

SeedRandom[];
RandomWalk = Flatten[Table[{x,y,z}={dist Sqrt[1 - cosinus^2]Cos[phi],dist Sqrt[1 - cosinus^2]Sin[phi],dist cosinus};
{u,v, w}={0.0, 0.0, 0.0};
dist = RandomReal[{5,10}];
phi = RandomReal[{0,2Pi}];
cosinus = RandomReal[{-1,1}];
velocity = Abs[RandomReal[NormalDistribution[0,s]]];

Line[NestList[(
u+=velocity Sqrt[1 - cosinus^2]Cos[phi];
v+=velocity Sqrt[1 - cosinus^2]Sin[phi];
w+=velocity cosinus;
#+IterationStep{u,v, w})&,{x,y, z},NumberOfSteps]],{s,0.25,0.75,0.007}][[All,1]],1];

CloudsParticles = Flatten[Table[(#+RandomReal@LaplaceDistribution[0,thickness])&/@#,{Max[1,IntegerPart@RandomReal@NormalDistribution[pointsmean,pointssd]]}]&/@RandomWalk, 1];

max=Max[Norm/@CloudsParticles];
NormalizedParticles = CloudsParticles/max;

MinSeparation[p_] := SpriteOverlap SpriteSize[p];
KeepPoint[{p_,q_}] := Norm[p]<Norm[q]||Norm[p-q]>MinSeparation[p];
FilterOnce[pts_] := With[{nf=Nearest[pts]},Select[pts, KeepPoint[nf[#,2]]&]];
PointsCoords = FixedPoint[FilterOnce,NormalizedParticles];

ListPointPlot3D[PointsCoords,BoxRatios->{1,1,1},ImageSize->800,SphericalRegion->True,PlotStyle->{Blue,PointSize[Small]}]


Here's a sample of the output. As you can see, this isn't a radial distribution :

The mistake most probably lies in the RandomWalk declaration, but I can't see it. Anyone has an idea of what may be wrong ?

Take note that I'm using Mathematica 7.0 only.

EDIT :

I must admit that this method isn't a clever way of defining a random distribution of points around radial lines. I'll have to do it differently.

• Could you please specify what is a "radial" random walk? Mar 18, 2013 at 14:48
• I mean a motion on a radial line only, so : steps forward, step back, etc, but toward (or away) the origin of coordinates.
– Cham
Mar 18, 2013 at 14:59

Put the line

{x,y,z}={dist Sqrt[1 - cosinus^2]Cos[phi],dist Sqrt[1 - cosinus^2]Sin[phi],dist cosinus};


Behind

velocity = Abs[RandomReal[NormalDistribution[0,s]]];


and the other expressions that set your variables, rather than before it. With

MinSprite := 0.006; (* min radius of sprites *)
MaxSprite := 0.03; (* max radius of sprites *)
SpriteOverlap := 0.75; (* min separation between sprites *)
IterationStep := 0.1;
NumberOfSteps := 20;
thickness = 0.09;
pointsmean = 20;
pointssd = 12;


You will see that setting

randomLines =
Table[
{u, v, w} = {0.0, 0.0, 0.0};
dist = RandomReal[{5, 10}];
phi = RandomReal[{0, 2 Pi}];
cosinus = RandomReal[{-1, 1}];
velocity = Abs[RandomReal[NormalDistribution[0, s]]];

{x, y, z} =
dist { Sqrt[1 - cosinus^2] Cos[phi], Sqrt[1 - cosinus^2] Sin[phi],
cosinus};

Line[
NestList[
(u += velocity Sqrt[1 - cosinus^2] Cos[phi];
v += velocity Sqrt[1 - cosinus^2] Sin[phi];
w += velocity cosinus;
# + IterationStep {u, v, w}) &,
{x, y, z},
NumberOfSteps
]
]

,
{s, 0.25, 0.75, 0.007}

]


and then doing

Graphics3D@randomLines


yields a picture with radial random lines.

Remark

Note that

randomLines =
Table[
{u, v, w} = {0.0, 0.0, 0.0};
dist = RandomReal[{5, 10}];
velocity = Abs[RandomReal[NormalDistribution[0, s]]];

{x, y, z} = dist RandomReal[{-1, 1}, 3];

Line[
NestList[
({u, v, w} = {u, v, w} + velocity {x, y, z};
# + IterationStep {u, v, w}) &,
{x, y, z},
NumberOfSteps
]
]
,
{s, 0.25, 0.75, 0.007}
];


also creates some radial random lines. Just as a side remark.

• Thanks a lot for the answer ! The first suggestion solved my issue. But I don't understand the rest of the answer ; I don't see any difference with my code and the last part of your answer.
– Cham
Mar 18, 2013 at 14:53
• @Cham haha yeah it must have been some copy paste error :). It should be ok now. Mar 18, 2013 at 14:55
• Ok, everything appears to be fine. Thanks again for your help. It's very appreciated !
– Cham
Mar 18, 2013 at 14:57
• @Cham note that all of this can be made a lot faster if you want. It is probably also good to note that the definitions probably yield strange random directions and that it is probably "better" to generate random angles and then apply a Cos or Sin to it. Note that in your code you make the Lines and then throw away the heads Line after that, which is a bit pointless :). Mar 18, 2013 at 14:58
• I'm not sure to understand. About the random angle, I used cosinus as a random variable between -1 and 1 to be sure to have an uniform distribution on a sphere. And yes, my code is a bit slow. What would you suggest ? Notice that the lines should have a thickness (random points around the radial lines).
– Cham
Mar 18, 2013 at 15:01