develooper Front page | perl.beginners | Postings from February 2009

self centering random walk within defined range

Thread Next
From:
rob
Date:
February 28, 2009 04:29
Subject:
self centering random walk within defined range
Message ID:
20090228122917.GA14098@goto10.org
hi,

i am finding something couter intuative about randomness and search for a 
solution.

i have a bit of code that randomly selects a number from an array and then adds 
it to the previous number.  I have two positive numbers and their negitives  qw(1 2 -1 -2)

i expected the below code to hover around 0 up and down a bit in each direction 
- but it very quickly shoots of into big numbers.

i would like to have a way to contain the output range to  between -45 and 45 for example.

anyone have a clue how to do this?

thanks

rob

#!/usr/bin/perl
use strict;
use warnings;

my @intervalset = qw(1 2 -1 -2);

my $current = 0;

my $t = 1;

while ($t==1){

my $rndi  = $intervalset[rand @intervalset];

$current = ($rndi+$current);

print $current,"\n";

}


--------------
rob@goto10.org
rob.goto10.org
--------------

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About