Forum: Ruby More random, please ! :)

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.
Meino Christian C. (Guest)
on 2006-04-06 22:29
(Received via mailing list)
Hi,

 recently I asked, how one would distribute all letters of
 the alphabet randomly and each statistically equal handled
 (oh...damn...this looks like awful german English...sorry...)

 And the solution was too simple, as I would have thought
 of it...hrrmmm...

 And now I fear, that the next question will result again in
 such simple kind of answer since we all program ruby here...;)

 How can I distribute all 25 letters (I will skip the "J" for this)
 over a 5x5 matrix (two dimensional array) that way, that each letter
 is handled equally from the statistical point of view ?

 Thank you very much for your help in advance !
 Ruby!
 mcc
Pit C. (Guest)
on 2006-04-06 22:42
(Received via mailing list)
Meino Christian C. schrieb:
>  How can I distribute all 25 letters (I will skip the "J" for this)
>  over a 5x5 matrix (two dimensional array) that way, that each letter
>  is handled equally from the statistical point of view ?

require "enumerator"

(("A".."Z").to_a-["J"]).sort_by{rand}.enum_slice(5).to_a

Regards,
Pit
Logan C. (Guest)
on 2006-04-06 22:45
(Received via mailing list)
On Apr 6, 2006, at 2:28 PM, Meino Christian C. wrote:

>  such simple kind of answer since we all program ruby here...;)
>
% irb
irb(main):001:0> require 'enumerator'
=> true
irb(main):002:0> letters = ("a".."z").to_a
=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
irb(main):003:0> letters.delete("j")
=> "j"
irb(main):004:0> letters2d = letters.sort_by { rand }.to_enum
(:each_slice, 5).to_a
=> [["v", "t", "p", "c", "e"], ["h", "q", "z", "u", "n"], ["k", "m",
"y", "g", "i"], ["x", "o", "s", "w", "d"], ["a", "b", "l", "r", "f"]]
irb(main):005:0> require 'pp'
=> true
irb(main):006:0> pp letters2d
[["v", "t", "p", "c", "e"],
["h", "q", "z", "u", "n"],
["k", "m", "y", "g", "i"],
["x", "o", "s", "w", "d"],
["a", "b", "l", "r", "f"]]
=> nil
Simon Kröger (Guest)
on 2006-04-06 23:31
(Received via mailing list)
> [...]
>  How can I distribute all 25 letters (I will skip the "J" for this)
>  over a 5x5 matrix (two dimensional array) that way, that each letter
>  is handled equally from the statistical point of view ?


Hi Christian,

you got some answers already, but of course there is always another fun
way to do stuff with ruby:

letters = [*('A'..'Z')] - ['J']
p (0...5).map{(0...5).map{letters.slice!(rand(letters.size))}}

cheers

Simon
This topic is locked and can not be replied to.