Tag Info

New answers tagged

4

Walking infinitely in a random and acceptable direction within a rectangle on button click, stepSize = 1. DynamicModule[{newDir, walk = {{0, 0}}, oldPos, newPos = {0, 0}, acceptQ = -20 <= #[[1]] <= +20 && -10 <= #[[2]] <= +10 &}, { Button["Next Step", oldPos = newPos; newPos = {\[Infinity], \[Infinity]}; ...


24

To answer your question: I don't think it's a bad or good idea to use If. It depends on how you do it. To demonstrate I'll use If combined very powerfully with Mathematica 10's ability to tell if a point is inside a specified region or not. step[position_, region_] := Module[{randomStep}, randomStep = RandomChoice[{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}]; ...


6

I chose the WienerProcess as the underlying random process, as this will simulate a Brownian motion. Until Boundary Hit Module[{rd = Transpose @ RandomFunction[WienerProcess[], {0, 1000, .01}, 2]["States"], length}, length = LengthWhile[rd, # ∈ Rectangle[{-2, -2}, {+2, +2}] &]; ListPlot[rd[[;; length]], Joined -> True, Mesh -> All, PlotRange ...


13

I suggest using Mod - a natural thing for looped boundary conditions on a torus. Finite torus surface area is your bounded region. 2D random walk generally is simple: walk = Accumulate[RandomReal[{-.1, .1}, {100, 2}]]; Graphics[Line[walk], Frame -> True] Confinement to square region {{0,1},{0,1}} would be simple in principle with Mod[walk,1] ...


7

Here's my implement of a random walk within a circle using If and FoldList. Please see @Pickett's answer for more thorough implementation for arbitrary regions. Code updated to flesh out behavior near edge of region (if a step becomes out of bound, the current position will randomly look for the other step types that would stay in the region). I also added ...


0

The easiest way is to define the vertices sequentially. x=RandomReal[{0,1/2},1000] y=RandomReal[{0,1/2-x},1000] z=RandomReal[{-1,-2(x+y)},1000]


4

Ray Koopman's answer to Uniformly distributed n-dimensional probability vectors applied to a simplex with given vertices leads to the following way to get (exactly) n uniformly distributed random points in the simplex (and do it quickly): pts = #.vertices/Total[#, {2}] &@ Log @ RandomReal[1, {n, 4}]; Here are 2000 points in the OP's simplex: vertices ...


5

The following seems to work: Cases[RandomReal[{-1, 1}, {1500000, 3}], {x_, y_, z_} /; 0 < x < 0.5 && 0 < y < 0.5 - x && -1 < z < -2 (x + y)] OR Select[RandomReal[{-1, 1}, {1500000, 3}], 0 < #[[1]] < 0.5 && 0 < #[[2]] < 0.5 - #[[1]] && -1 < #[[3]] < -2 (#[[1]] + #[[2]]) &] Here ...


4

Here is a quick (and dirty?) implementation of the Laplace distribution. Relationship to the uniform distribution as given on Wikipedia: randomLaplace = With[{$MachineEpsilon = $MachineEpsilon}, Compile[{{µ, _Real, 0}, {b, _Real, 0}, {n, _Integer, 0}}, With[{u = RandomReal[{-1/2, 1/2} (1 - $MachineEpsilon), n]}, µ - b Sign[u] Log[1 - 2 Abs[u]] ] ...


8

Another option to the answer posted by @Andy Ross cropped up in a recent question of mine about corrupting an image with Poisson noise. In my own answer, I made use of LibraryLink to utilise the distributions built into C++, which you can find here: http://www.cplusplus.com/reference/random/ This was especially useful in my case because Poisson noise in an ...


0

I tried several ways to save time and make it more effective: McVol1[num_] := Module[{hit, miss, index, x, y, z}, hit = 0; miss = 0; For[index = 1, index <= num, ++index, {x, y, z} = RandomReal[{-1, 1}, 3]; If[x^2 + y^2 + z^2 <= 1, ++hit]]; hit/num] Print["time and value...... :", Timing[N@McVol1[100000]]] Result is time and ...


17

@Simon Woods points out in a comment that: In fact the delay on the initial run is caused by compiling code to provide the Poisson distribution :-) You can look at ImageColorOperationsDumpiImageEffectPoissonNoise to see how it works internally. Now, although PoissonDistribution can't be compiled, there's nothing stopping the use of my own C++ ...


3

Another way is {#, RandomReal[#]} & /@ RandomVariate[TriangularDistribution[{0, 1}, 1], 1000] To see we do have a uniform distribution: PDF[ TransformedDistribution[{x, y}, { x  TriangularDistribution[{0, 1}, 1], y  UniformDistribution[{0, b}] }] /. b -> x, {x, y}]


5

Generate random pairs, and reverse-sort each one: Sort[#, Greater] & /@ RandomReal[1, {10, 2}] or, for fun, generate two numbers between 0 and 0.5, and add the second to the first: RandomReal[0.5, {10, 2}].{{1, 0}, {1, 1}}


12

RandomReal[1, {100, 2}] /. {x_, y_} /; y > x :> {y, x} The graphics below confirm that you retain a uniform distribution



Top 50 recent answers are included