From: "F. Senault" Date: 2007-03-12T06:50:07+09:00 Subject: Re: [QUIZ] SimFrost (#117) Le 11 mars 2007 � 21:07, Christoffer Lern� a �crit : > Anyone else with a more elegant solution to the problem? I'd use this (only moderately tested) : #! /usr/local/bin/ruby p = 0.3 w = 100 h = 100 t = h * w v = (t * p).floor g = Array.new(v, '.') + Array.new(t - v, '.') 0.upto(t - 1) do |i| j = i + rand(t - i) z = g[j] ; g[j] = g[i] ; g[i] = z # g[j], g[i] = g[i], g[j] end (And then split the grid in w chunks.) The fun part is that the version using a parallel assignment (commented) is actually much slower (about two times) than the version using the temporary variable, to the point that it was beaten by sort_by (which is supposed to be O(n*log(n)) versus O(n) for my versions). 200*200 : user system total real scramble / temp : 15.281250 0.000000 15.281250 ( 15.587794) scramble / swap : 31.398438 0.007812 31.406250 ( 32.220921) sort_by rand : 28.625000 0.101562 28.726562 ( 29.168337) 500*500 : user system total real scramble / temp : 97.195312 0.000000 97.195312 ( 98.037395) scramble / swap : 297.937500 0.929688 298.867188 (302.746914) sort_by rand : 213.906250 0.468750 214.375000 (217.172667) Fred -- Young at heart an' it gets so hard to wait When no one I know can seem to help me now Old at heart but I mustn't hesitate If I'm to find my way out (Guns n' Roses, Estranged)