Forum: Ruby (no subject)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cdb3ec23d3ee6366dcffe49150a0a9e9?d=identicon&s=25 Meino Christian Cramer (Guest)
on 2006-03-30 22:36
(Received via mailing list)
Hi;

 I am looking for an algorithm, which spread all 26 letters of the
 alphabet over an array of 26 places randomly. All letters should be
 statistically equally handled.

 Thank you very much for any helpful reply ! :)

 Keep hacking!
 mcc
Re:
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-03-30 22:39
(Received via mailing list)
On Mar 30, 2006, at 2:33 PM, Meino Christian Cramer wrote:

> Hi;
>
>  I am looking for an algorithm, which spread all 26 letters of the
>  alphabet over an array of 26 places randomly. All letters should be
>  statistically equally handled.

Well, this is only as good as the random number generator, but it
sure is easy:

 >> ("a".."z").sort_by { rand }
=> ["s", "m", "q", "j", "f", "r", "z", "e", "l", "k", "y", "v", "g",
"d", "x", "p", "w", "a", "b", "c", "i", "t", "h", "o", "u", "n"]

Hope that helps.

James Edward Gray II
Re:
Cdb3ec23d3ee6366dcffe49150a0a9e9?d=identicon&s=25 Meino Christian Cramer (Guest)
on 2006-03-30 22:57
(Received via mailing list)
From: James Edward Gray II <james@grayproductions.net>
Subject: Re:
Date: Fri, 31 Mar 2006 05:39:15 +0900

Hi James,

 again the ruby-effect: The way to code things in ruby is just too
 straight forward and (only meant positively) simple.

 Again, I was thinking way too complex.....with no result at all.

 Thanks a lot! :)

 Have a nice time!
 mcc
Re:
Cdb3ec23d3ee6366dcffe49150a0a9e9?d=identicon&s=25 Meino Christian Cramer (Guest)
on 2006-03-30 23:13
(Received via mailing list)
From: James Edward Gray II <james@grayproductions.net>
Subject: Re:
Date: Fri, 31 Mar 2006 05:39:15 +0900

Hi,

 sorry, me again.

 If I want not only "a".."z" but also "A".."Z" I thought (from the
 logical point of view...) that

 >> a=("A".."z").sort_by { rand }

 would work, but it gaves me:

 >> a
 => ["P", "D", "Q", "N", "H", "A", "M", "X", "G", "C",
     "E", "L", "I", "S", "B", "J", "F", "Y", "T", "Z", "V", "K", "O",
"U", "R", "W"]

 which isn't exactly, what I have exspected....

 Unfortunately

 a=("A".."Z").sort_by { rand } + ("a".."z").sort_by { rand }

 would first randomize A->Z and then a->z and dont mix both.

 Or am I not thinking too straight forward again ;)  ?

 Keep hacking!
 mcc
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-03-30 23:22
(Received via mailing list)
Can you please title your messages so others know what we are talking
about?

On Mar 30, 2006, at 3:11 PM, Meino Christian Cramer wrote:

> From: James Edward Gray II <james@grayproductions.net>
> Subject: Re:
> Date: Fri, 31 Mar 2006 05:39:15 +0900
>
> Hi,
>
>  sorry, me again.
>
>  If I want not only "a".."z" but also "A".."Z"

We can just add the Arrays of letters, then randomize:

 >> (("a".."z").to_a + ("A".."Z").to_a).sort_by { rand }
=> ["h", "j", "m", "x", "z", "O", "n", "r", "T", "F", "N", "k", "U",
"a", "W", "p", "V", "E", "L", "A", "v", "D", "J", "P", "y", "C", "g",
"u", "S", "i", "w", "q", "l", "K", "c", "B", "Z", "s", "t", "Y", "R",
"e", "d", "o", "M", "I", "X", "f", "b", "G", "Q", "H"]

Hope that helps.

James Edward Gray II
9358cc96c46055cd68d4a76a9aefe026?d=identicon&s=25 Daniel Harple (Guest)
on 2006-03-30 23:22
(Received via mailing list)
On Mar 30, 2006, at 11:11 PM, Meino Christian Cramer wrote:

> I want not only "a".."z" but also "A".."Z"

Try:

(("a".."z").to_a + ("A".."Z").to_a).sort_by { rand }

-- Daniel
Re:
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 unknown (Guest)
on 2006-03-30 23:25
(Received via mailing list)
On Mar 30, 2006, at 4:11 PM, Meino Christian Cramer wrote:
>  If I want not only "a".."z" but also "A".."Z" I thought (from the
>  logical point of view...) that
>
>>> a=("A".."z").sort_by { rand }

a = (('a'..'z').to_a + ('A'..'Z').to_a).sort_by { rand }

Gary Wright
Re:
24d3102d656a4654db23d28382a2d6f0?d=identicon&s=25 Timothy Bennett (Guest)
on 2006-03-30 23:25
(Received via mailing list)
a = ("A".."Z" + "a".."z").sort_by { rand }

Tim
Re:
54f9c5392e17646bc29fe6435b55f609?d=identicon&s=25 baumanj@gmail.com (Guest)
on 2006-03-30 23:25
(Received via mailing list)
Not quite as elegant, but this should work:

(('a'..'z').to_a + ('A'..'Z').to_a).sort_by { rand }
Re:
Cdb3ec23d3ee6366dcffe49150a0a9e9?d=identicon&s=25 Meino Christian Cramer (Guest)
on 2006-03-31 06:18
(Received via mailing list)
From: Timothy Bennett <timothy.s.bennett@gmail.com>
Subject: Re:
Date: Fri, 31 Mar 2006 06:22:37 +0900

Hi Tim,

 this gives an error with my recent "stable-snapshot-ruby":

   >> a = ("A".."Z" + "a".."z").sort_by { rand }
   SyntaxError: compile error
   (irb):1: syntax error, unexpected tDOT2
   a = ("A".."Z" + "a".."z").sort_by { rand }
                        ^
           from (irb):1
   >>

 Keep hacking!
 mcc
This topic is locked and can not be replied to.