# More random, please ! :)

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 ?

Ruby!
mcc

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

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

[…]
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