PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > briefest method of generating a list of random numbers?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
briefest method of generating a list of random numbers?

Réponse
 
LinkBack Outils de la discussion
Vieux 26/05/2008, 09h45   #1
Boris Schmid
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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/.

  Réponse avec citation
Vieux 26/05/2008, 10h16   #2
Mikael Høilund
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: briefest method of generating a list of random numbers?


On May 26, 2008, at 10:45, Boris Schmid wrote:

> Hi all,
>
> I wondered if there was a more compact method of generating things =20
> like
> a random string, or a random list of numbers than I'm using now (using
> ruby 1.9.0-1)


Array(100) { rand }

The block is evaluated for each element.

--=20
Mikael H=F8ilund=

  Réponse avec citation
Vieux 26/05/2008, 10h17   #3
Axel Etzold
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: briefest method of generating a list of random numbers?


-------- Original-Nachricht --------
> Datum: Mon, 26 May 2008 17:45:57 +0900
> Von: Boris Schmid <boris@bagofsouls.com>
> 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

  Réponse avec citation
Vieux 26/05/2008, 10h34   #4
Boris Schmid
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: briefest method of generating a list of random numbers?


> But what's wrong with the first - it looks fairly compact to me:
>
> string = "" ; 100.times { string << (rand(26) + 97).chr}
>


For strings I'll have to use the one above, but arrays now read a bit
more natural (to my eyes). The size doesn't matter that much, but it is
simpler to understand.

population = Array.new(POP_SIZE) {
function_that_generates_a_random_individual }

vs the old

population = [] ; POP_SIZE.times {population <<
function_that_generates_a_random_individual }

Thanks both of you!
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 26/05/2008, 10h43   #5
Lars Christensen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: briefest method of generating a list of random numbers?

On May 26, 11:16 am, Mikael Høilund <mik...@hoilund.org> wrote:
> On May 26, 2008, at 10:45, Boris Schmid wrote:
>
> > 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) { rand }


Don't you mean

Array.new(100) { rand }

Array(arg) creates an array equal to 'arg' (if it responds to .to_ary)
or an array containing precisly 'arg' otherwise. It never yields.

There is also

(1..100).map { rand }

And for strings

(1..100).map { (rand(26)+97).chr }.join

Lars
  Réponse avec citation
Vieux 26/05/2008, 10h43   #6
Boris Schmid
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: briefest method of generating a list of random numbers?

Boris Schmid wrote:

>> string = "" ; 100.times { string << (rand(26) + 97).chr}
>>

>
> For strings I'll have to use the one above,


Duh. I can just use the Array.new, and append a .join at the end to get
the random string. And Axel, I'm generating quite a lot of arrays in my
small prog, and using this shorter way makes it indeed clearer . It is
definitly worth it.

Example:


filter = Array.new(WINDOW) { bucket = Array.new(AMINO) {|i| (97 +
i).chr} }

was

filter = []
WINDOW.times {bucket = [] ; AMINO.times {|i| bucket << (97 + i).chr}
; filter << bucket}
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 26/05/2008, 10h45   #7
Robert Klemme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: briefest method of generating a list of random numbers?

2008/5/26 Boris Schmid <boris@bagofsouls.com>:
>
>> But what's wrong with the first - it looks fairly compact to me:
>>
>> string = "" ; 100.times { string << (rand(26) + 97).chr}
>>

>
> For strings I'll have to use the one above,


Note that String#<< understands ints as being character codes - so you
do not need the .chr:

irb(main):005:0> str = 100.to_enum(:times).inject("") {|s,| s << (?a +
rand(26))}
=> "uepizlkepyoxxfkefosxxptvcpdiuwyexecngmftnidlyfnhq gmtndbzthhykdjjiyayzwvhatovzbxzqkhclnxwbqbsoxpszwk n"
irb(main):006:0>

I believe in 1.9 you can as well do

str = 100.times.inject("") {|s,| s << (?a + rand(26))}

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

  Réponse avec citation
Vieux 26/05/2008, 10h50   #8
Calamitas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: briefest method of generating a list of random numbers?

On Mon, May 26, 2008 at 11:34 AM, Boris Schmid <boris@bagofsouls.com> wrote:
>
>> But what's wrong with the first - it looks fairly compact to me:
>>
>> string = "" ; 100.times { string << (rand(26) + 97).chr}
>>

>
> For strings I'll have to use the one above, but arrays now read a bit
> more natural (to my eyes). The size doesn't matter that much, but it is
> simpler to understand.


You could also do

Array.new(100) { 97 + rand(26) }.pack("c*")

Peter

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h05.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15399 seconds with 16 queries