[考え方]

1次元ランダムウォークの延長で、2次元(x,y)座標で移動ができる場合を考えます。
原点(0,0)から出発して、一回の試行で、確率0.25で、x=+1,x=-1,y=+1,y=-1のいずれかに
移動することが可能であるとします。

つまり、いま、 (x_i,y_i)だとすると、i+1では
(x_i + 1, y_i)
(x_i - 1, y_i)
(x_i, y_i + 1)
(x_i, y_i - 1)
のどれかの状態になるとします。斜めの移動を許さずに上下左右(次元をNとすると2Nの自由度)で移動をすることとします。

[プログラム]

こんな感じになります。

=begin

2-dimensional random walk
Show each trial
2008.3.19

=end

require "tk"
include Math


Geometry = Struct.new(:x, :y)

class TwoDimensionalRandomWalk

  def initialize( step = 100)
    @step = step
    @path = []
    srand
    each_step
  end
  
  def each
    @path.each{|geometry|
      yield(geometry)
    }
  end
   
  def show
    @path.each { |geometry|
      printf( "%5d\t%5d\n",geometry.x,geometry.y)
    }
  end

  
private

  def each_step
    x_tmp = 0
    y_tmp = 0
    i = 1
    while i <= @step do
      tmp = rand
      
      if tmp < 0.25  then
        x_tmp += 1
        
      elsif tmp < 0.5 then
        x_tmp -= 1
      elsif tmp < 0.75 then  
        y_tmp += 1
      else
        y_tmp -= 1
      end
      @path << Geometry.new(x_tmp, y_tmp)
      i +=1
    end
      @path
  end
    
end


class TkDistribution
  include Tk

  def initialize(width=100, height=100,rectsize=4)
    @rectsize = rectsize
    @width = width
    @height = height
    
    # Main Window
    @canvas = TkCanvas.new(nil,
                          'width'=>(@width-1)*@rectsize,
                          'height'=>(@height-1)*@rectsize,
                          'borderwidth'=>1,
                          'relief'=>'sunken')

    @rectangles = []
        
    @canvas.pack
   
  end

  def run(a,color)
    display(a,color)
    mainloop
  end
  
  def display(a,color)

    a.each {|geometry|
    
      pos_x =  (0.5 *  @width  + geometry.x )* @rectsize
      pos_y = ( 0.5 *  @height + geometry.y )* @rectsize
     
      @rectangles << TkcRectangle.new(@canvas,
                                                pos_x  ,
                                                pos_y,
                                                pos_x  + @rectsize -1 ,
                                                pos_y  + @rectsize -1,
                                                'fill' =>color)
    } 

  end


end

  
  



##############################################################################
# Main Routine
##############################################################################

if __FILE__ == $0


step = 1000

 a = TwoDimensionalRandomWalk.new(step)
 
 #a.show
  
 
 g = TkDistribution.new(100,100)
 g.run(a,"blue")

end

[結果]

グラフィックのY軸が天地逆だと思います。毎回実行するとそれぞれのパスをTkで描画します。

TwoDimensionalRandomWalk::showで、pathを表示することができます。
意外と遠くまでは行かないで原点近くをうろうろしているのがわかります。

3次元に拡張したければ、上下左右前後と3次元座標で各試行で確率1/6で同じことをしてあげればいい。
4次元は、2*4=8の方向に確率1/8に拡張すればOKです。(絵は描けませんが。)

数学的な連続極限では拡散方程式に従います。