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

self centering random walk within defined range

Thread Next
February 28, 2009 04:29
self centering random walk within defined range
Message ID:

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

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?



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";



Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About