Closed Thread Icon

Topic awaiting preservation: An elegant way to mix arrays arrays... (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=25193" title="Pages that link to Topic awaiting preservation: An elegant way to mix arrays arrays... (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: An elegant way to mix arrays arrays... <span class="small">(Page 1 of 1)</span>\

 
Grand Mamamouchi
Obsessive-Compulsive (I) Inmate

From:
Insane since: Nov 2004

posted posted 03-06-2005 09:40

Hi !

I am looking for an elegant way to mix arrays arrays.

I admit i am fairly proud to have found the hereunder script. Unfortunately,
it breaks the lines.

Thanx for your help !

code:
array=[
['Bernard','Bernard'],
['Jean','Jean'],
['Jocelyne','Jocelyne'],
['Maud','Maud'],
['Rudolf','Rudolf']
]

function aleate()
{
return Math.pow(-1, Math.round(2*Math.random()))
}

alert(array.sort(aleate))

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 03-06-2005 12:19

Grand Mamamouchi: Hello and welcome in the Asylum.

What do you mean by "it breaks the lines" ? If you mean that the content of the array appear on a straight line with the alert( ), it is 100% normal. That is the way arrays are displayed in an alert( ). To prove that, simply put an alert( array ) before using the alert(array.sort(aleate)). If you want a pretty display, you'll need few lines of code for that, to browse each member of the Array and add a LF character.

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 03-06-2005 14:02

Oh, and you could shorten your aleate( ) function. All it needs to do is to return a positive or negative number at random. For the record, I've included a dump( ) method on the Array object, which gives the following script:

code:
array =
[
"toto",
78,
['Bernard','Bernard'],
['Jean','Jean'],
['Jocelyne','Jocelyne'],
['Maud','Maud'],
['Rudolf','Rudolf']
]

Array.prototype.dump = function( indent )
{
var bla = ""
if( !indent )
indent = ""
for( var i in this )
{
var typeOf = typeof( this[ i ] )
if( typeOf!="function" )
{
bla += indent+ i +" -{"
if( this[i].dump )
bla +="array}->\n"+ indent +"{\n"+ this[i].dump( indent+"\t" )+ indent +"}\n"
else
bla += typeOf +"}-> "+ this[i] +"\n"
}
}
return bla
}

function aleate()
{
return 2*Math.random()-1
}

alert( array.sort(aleate).dump() )



Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 03-06-2005 20:11

As a side note, what's the point of the random sort? Wouldn't it be better to do an O(n) permutation if randomness is needed? Besides, I think it's important for a sort function that the comparison returns the same value when called on the same elements more than once; you might not be getting an even distribution.


 

liorean
Bipolar (III) Inmate

From: Umeå, Sweden
Insane since: Sep 2004

posted posted 03-06-2005 23:41

If the only problem is the presentation,

code:
alert(array.sort(aleate).join('\n'));

But I've seen this question asked in codingforums, webxpertz, sitepoint, webdeveloper forums and here. I've not seen any reply by the original poster anywhere.

--
var Liorean = {
prototype: XHTMLGuru.prototype,
abode: "http://codingforums.com/",
profile: "http://codingforums.com/member.php?u=5798"};

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 03-07-2005 00:15

lioran: it's always a pleasure to see a dead simple solution doing what I spent 19 lines to do

Slime: Indeed O(n) is better.

code:
Array.prototype.shuffle = function()
{
for( var i=1; i<this.length; i++ )
if( 2*Math.random()>1 )
{
var tmp = this[i]
this[i] = this[i-1]
this[i-1] = tmp
}
return this
}

the random function could be improved to get a better distribution ( gaussian, poisson, perlin ... )

liorean
Bipolar (III) Inmate

From: Umeå, Sweden
Insane since: Sep 2004

posted posted 03-07-2005 14:59

Poi:That's probably a lot more effective than my solution...

code:
Array.prototype.shuffle=function(){
var
l=this.length,
a=[];
while(l-->0)
a.push(this.splice(Math.floor(l*Math.random()),1));
return this.concat(a);
}



--
var Liorean = {
abode: "http://codingforums.com/",
profile: "http://codingforums.com/member.php?u=5798"};

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 03-07-2005 15:14

the previous function was not random enough :

code:
Array.prototype.shuffle = function()
{
var
L = this.length,
random

while( --L )
if( L-(random = Math.round(L*Math.random())) )
{
var tmp = this[L]
this[L] = this[random]
this[random] = tmp
}

return this
}

To get a "random" function that shuffles the array the same way, we could use pseudo-random number generator and generate a seed ( eventually based on a hash key made from the datas of the array ) on the first call of the the suffle( ) method.



(Edited by poi on 03-07-2005 15:21)

« BackwardsOnwards »

Show Forum Drop Down Menu