From: "Daniel Malcolm Webb [dbw]" Date: 2008-12-01T21:02:16+09:00 Subject: Re: Control Structures Hi Jakub, What would be the quickest way of doing it? (in terms of time and memory?) Thanks, Dan -----Original Message----- From: Jakub Pavlík jn. [mailto:severus@post.cz] Sent: 01 December 2008 11:53 To: ruby-talk ML Subject: Re: Control Structures > G'day all, I'm new to Ruby, been studying Software Engineering at > university and they have been teaching in Java. > > As such, I'm trying to learn as few of the basics by going through and > comverting smoe of the programs I made into Ruby. At the moment, I'm > struggling a bit with the control structures, if-then-else is fine, > switch/case is fine. I'm just a little confused about the for and > while/dowhile loops. > > I'm trying to make a "infinite monkeys" simulation, i.e. initiate an > array of 'x' length and then fill it with random numbers, then check to > see if those numbers are in order, if not, repeat the process. More or > less anyway. > > At the moment I'm using this loop to fill the array: > for num in (0..Integer(a)) > monkey[num]=Integer(rand*5+1) > end > variable 'a' is input by the user to determine how big a sequence, i.e. > 5 numbers in a row. > > Is this the easiest way of doing things? > And then I could use a similar loop to go through and check to see if > each number is in order, but again, is there a simpler way to check? > > Thanks in advance. > TJ > -- > Posted via http://www.ruby-forum.com/. monkey = [] a = gets begin 0.upto(a.to_i) do |num| monkey.push((rand*5+1).to_i) end end while a.sort != a (Of course it is not the most time- and memory- effective way...) Jakub Pavlik -- Zatímco velbloud dvouhrbý má hrby dva, Dromedár má jen jeden.