Re: self centering random walk within defined range



Rob Dixon said :
rob@xxxxxxxxxx wrote:
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";

}

You are falling for the fabled 'law of averages' that tells people they are more
likely to win the lottery after losing many times. The sum will not hover around
its starting value as each random number is independent. If your total has
reached, say, 100 then you are still no more likely to roll negative numbers
than positive ones.

The expected, or mean value of the rolls is zero, but the variance of the total
increases with the number of rolls and so you will see it stray further and
further from zero as your program continues.

so i guess what i would need to do would be:
after each positive number weight, the probability in favour of the next number
being negative and vica versa. then maybe things wont stray so far from zero.


Please explain what numbers you are trying to generate. If you want an even
distribution of integers from -45 to 45 then simply use

my $num = int(rand 91) - 45, "\n";

basically the numbers i am generating are musical intervals which sum together
to give a string of pitches (melody) - things straying to far from 0 which in
this case could be the middle key of a piano means that the melody soon runs
outside the range of the instrument unless i intervene. at the moment i restrict
the movement to one octave using modulus % 12 - but this is not the solution i need.

i think i need to figure out how to change the probablities on each iteration.


Also, please indent your code inside a block to make it more legible.

sorry, yes i need to learn the right way of doing this - will consult my
o'reilly perl books.

And the
usual way of writing an indefinite loop is

while (1) {
:
}

ah yes - i have more stuff in my code - i pulled out this "bleeding chunk" to
try and keep my mail clear - nevertheless its always good to know these things
as i'm a real newb and most of my code is still cobbled together in a
frankenstein like manner :)


many thanks,

rob c

Attachment: signature.asc
Description: Digital signature



Relevant Pages

  • Re: self centering random walk within defined range
    ... i am finding something couter intuative about randomness and search for a ... The sum will not hover around ... The expected, or mean value of the rolls is zero, but the variance of the total ...
    (perl.beginners)
  • Re: Amazed at joker rolls on FIBS
    ... If I "got myself trapped into a way of thinking about FIBS dice", ... Rolls, as it is. ... these tables, I consider the randomness is quite, er... ...
    (rec.games.backgammon)
  • Re: Real numbers of entropy bits
    ... discerned (and the randomness itself extracted) without debiasing if the ... entropy of that distribution. ... "If I roll a fair 6-sided die once and get a 3, ... given short series of rolls containing mostly 1s is equiprobable to some ...
    (sci.crypt)
  • Re: PI random? Debate running in circles (you try making math jokes)
    ... Your question about randomness of Pi is no different ... still zero, because zero has no multiplicative identity. ... a bit of beauty to the the real number zero. ...
    (sci.math)
  • Re: Filer within Pivot Table
    ... Rob ... > search for hide zero value pivot table and in author put Debra Dalgleish ...
    (microsoft.public.excel.misc)