From: Axel Etzold Date: 2008-05-26T18:17:03+09:00 Subject: Re: briefest method of generating a list of random numbers? -------- Original-Nachricht -------- > Datum: Mon, 26 May 2008 17:45:57 +0900 > Von: Boris Schmid > An: ruby-talk@ruby-lang.org > Betreff: briefest method of generating a list of random numbers? > Hi all, > > I wondered if there was a more compact method of generating things like > a random string, or a random list of numbers than I'm using now (using > ruby 1.9.0-1) > > array = [] ; 100.times {array << rand} > string = "" ; 100.times {string << (rand(26) + 97).chr} > > The temptation is to use > > array = [rand] * 100 > string = (rand(26) + 97).chr * 100 > > but that only draws one random number a 100 times. > > Is there a bit of syntax that I missed? > -- > Posted via http://www.ruby-forum.com/. Hello Boris, > string = (rand(26) + 97).chr * 100 > > but that only draws one random number a 100 times. That's not correct - it draws a random number once, turns it into a letter and then repeats it 100 times - you could also write, e.g., string='x'*100 . But what's wrong with the first - it looks fairly compact to me: string = "" ; 100.times { string << (rand(26) + 97).chr} You could add a srand(); in the bracket to call different strings every time, but that's less compact :) Best regards, Axel -- Psssst! Schon vom neuen GMX MultiMessenger geh�rt? Der kann`s mit allen: http://www.gmx.net/de/go/multimessenger