From: Armin Roehrl Date: 2002-03-12T05:54:21+09:00 Subject: Re: painfully newbie question Hi Damien, I have not tried your code, but I think your problem is that you have 5 times the same individual in the Array. You can verify this by: put @alive=rand instead of alive Now look at your entries in the 1st round: Alive should always be the same. to fix this, create 5 instances of Individual e.g. population=Array.new for i in 0...5 population[i]=Individual.new(1,0) end puts "Bef.... hope this is what you wanted, A On Monday 11 March 2002 21:25, you wrote: > Last week I posted a question about using Ruby in > population ecology simulation, and I am very grateful > for the replies I got. It seems Ruby is exactly what > I am looking for. > > Of course, I have my first question/conundrum, and I > hope it isn't too painful for experienced Ruby/OO > programmers. > > I am trying to simulate "death" in an array of objects > ("Individuals") by tossing a "coin". However, for > some reason, I get results I am not expecting. Here > is my code: > > #Start > > class Individual > attr_writer :alive, :mother > def initialize(alive = 1,mother = 0) > @alive = alive > @mother = mother > end > > def to_s > "Alive: #{@alive} Pregnant: #{@mother} \n" > end > > def survival > if rand<0.5 > @alive = 0 > else > @alive = 1 > end > end > > end > > > population = Array.new(5,Individual.new(1,0)) > puts "Before random death trials: \n" > puts population.to_s > > puts "During random death trials: \n" > > for i in 0..(population.length-1) > population[i].survival > puts population[i].to_s > end > > puts "After random death trials: \n" > > puts population.to_s > > # End > > The output I get is: > > Before random death trials: > Alive: 1 Pregnant: 0 > Alive: 1 Pregnant: 0 > Alive: 1 Pregnant: 0 > Alive: 1 Pregnant: 0 > Alive: 1 Pregnant: 0 > During random death trials: > Alive: 1 Pregnant: 0 > Alive: 1 Pregnant: 0 > Alive: 1 Pregnant: 0 > Alive: 0 Pregnant: 0 > Alive: 0 Pregnant: 0 > After random death trials: > Alive: 0 Pregnant: 0 > Alive: 0 Pregnant: 0 > Alive: 0 Pregnant: 0 > Alive: 0 Pregnant: 0 > Alive: 0 Pregnant: 0 > > When I would expect to get: > > Before random death trials: > Alive: 1 Pregnant: 0 > Alive: 1 Pregnant: 0 > Alive: 1 Pregnant: 0 > Alive: 1 Pregnant: 0 > Alive: 1 Pregnant: 0 > During random death trials: > Alive: 1 Pregnant: 0 > Alive: 1 Pregnant: 0 > Alive: 1 Pregnant: 0 > Alive: 0 Pregnant: 0 > Alive: 0 Pregnant: 0 > After random death trials: > Alive: 1 Pregnant: 0 > Alive: 1 Pregnant: 0 > Alive: 1 Pregnant: 0 > Alive: 0 Pregnant: 0 > Alive: 0 Pregnant: 0 > > The value in "Alive" seem to be copied from the last > iteration into the array population once the for loop > is complete. > > I hope this question isn't too inane for the list, but > I am stumped and would appreciate any insight. > > Thank you in advance, > > Damien > > > __________________________________________________ > Do You Yahoo!? > Try FREE Yahoo! Mail - the world's greatest free email! > http://mail.yahoo.com/ -- Armin. ----------------------------------------------- Armin Roehrl, http://www.approximity.com Training, Development and Mentoring OOP, XP, Java, Ruby, Smalltalk, Datamining, Parallel computing, Webservices Koautor des ersten dt. Rubybuches http://approximity.com/rubybuch/