On Sat, Feb 28, 2009 at 12:22, <r...@goto10.org> wrote: > Rob Dixon said : >> r...@goto10.org 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 > > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.6 (GNU/Linux) > > iD8DBQFJqXLcyHhCfi3DkcIRAj1lAKCIs3flds9T8TfGpwpwuowlH4p3zwCgjIPq > gwBhaaZRhlmqxC0ruvA6bE4= > =OF5D > -----END PGP SIGNATURE----- > >
This seems close to what you want, but it needs work to make it take steps larger than 1: #!/usr/bin/perl use strict; use warnings; # 12345678901 # * # 5/11 chance of moving right or left # 1/11 chance of staying still # # 12345678901 # * # 4/11 chance of moving left # 6/11 change of moving right # 1/11 chance of staying still # # and so on sub random_move { my ($pos, $max) = @_; my $left = ($pos - 1)/$max; my $nomove = 1/$max; my $rand = rand; return $pos - 1 if $rand < $left; return $pos if $rand < $left + $nomove; return $pos + 1; #must be move right; } my $pos = 20; my $max = 41; while (1) { print " " x ($pos - 1), "*\n"; $pos = random_move($pos, $max); select undef, undef, undef, .25; } -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/