Topic awaiting preservation: An elegant way to mix arrays arrays... |
|
---|---|
Author | Thread |
Obsessive-Compulsive (I) Inmate From: |
posted 03-06-2005 09:40
Hi ! code: array=[ |
Paranoid (IV) Inmate From: France |
posted 03-06-2005 12:19
Grand Mamamouchi: Hello and welcome in the Asylum. |
Paranoid (IV) Inmate From: France |
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 = |
Lunatic (VI) Mad Scientist From: Massachusetts, USA |
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. |
Bipolar (III) Inmate From: Umeå, Sweden |
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. |
Paranoid (IV) Inmate From: France |
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 code: Array.prototype.shuffle = function() the random function could be improved to get a better distribution ( gaussian, poisson, perlin ... ) |
Bipolar (III) Inmate From: Umeå, Sweden |
posted 03-07-2005 14:59
Poi:That's probably a lot more effective than my solution... code: Array.prototype.shuffle=function(){
|
Paranoid (IV) Inmate From: France |
posted 03-07-2005 15:14
the previous function was not random enough : code: Array.prototype.shuffle = function() 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. |