There are faster ways of shuffling array elements, but this is nice and short:
Code:Array.prototype.shuffle=function(){
return this.sort(function(){return [-1,1][Math.floor(Math.random()*2)]})
}
Printable View
There are faster ways of shuffling array elements, but this is nice and short:
Code:Array.prototype.shuffle=function(){
return this.sort(function(){return [-1,1][Math.floor(Math.random()*2)]})
}
I dont know why you would resort to such a laggy script, this is 40 more bytes but much more efficient, the one you provided gets exponentially worse with longer arrays
Code:Array.prototype.shuffle=function(){var t=this,r=t.length,b=[],i=0,M=Math;while(i<r)b[i]=t[i++];while(i>0){r=M.floor(M.random()*i);t[--i]=b[r];b[r]=b[i]}return t}
I was curious as to what the level of randomness these two functions were so I set up a test page:
http://www.random.abrahamjoffe.com.a...huffletest.htm
Standard deviation is the measurement we are after, close to zero is best, check the results yourself they are quite surprising.
Geez, those results are shocking -- 1324 and 3124 got as high as x3. :eek:
Last time I use the sort approach.
I guess I will be forced to use your function or write a new one up.
But I'm sure your method is both fast and accurate.
String/Number.changeBase(from, to);
Changes a number from a base (first argument) to another base (second argument).
Code:Number.prototype.changeBase=(
String.prototype.changeBase=function(){
return parseInt(this, arguments[0]).toString(arguments[1]);
}
);
I have another revision of the rainbow script. It 's a String prototype function that creates DOM nodes (a containing span that contains one span per letter in the string). The reason for the containing span is so that another function can be called. This second function "moves" the rainbow by recoloring it with a different starting hue value.
I'm sure it's less efficient than some of the other rainbow stuff we've made, but it moves! Ooh!
Thats pretty awesome Hagane, though I dont see the purpose of making it a prototype function.
So I could post it in this thread :D
LOL,Quote:
Originally Posted by HaganeNoKokoro
I knew this was gonna happen sooner or later...
That RainbowSpan function is a nice function and deserves a proper place. With all due respect, post it here: Function Library
I'd add your RainbowSpan function to my index page, but it isn't a prototype function.
You guys have been watching my first post of this thread, right?
Yeah, I know it isn't really a prototype-function, I just figured I could get a rise out of you with that comment. I'll post the latest version in your other thread.
Does anybody have a function that would turn:
3.3333333333
into
[10,3]
as in 10/3, and the same for any other rational?
Thanks for the addition to my other thread, HaganeNoKokoro, that function really is an accomplishment -- I see many uses for it.
BigMoosie, I'm sure I can come up with something -- my TI-86 calc has a ►Frac function.
3.3333333333 Can be written as: 33333333333 / 1000000000
The only problem is that you need to make JavaScript understand that 3.3333333333 means 3.33... repeating.
Thus 33333333333 / 1000000000 means 10/3
Hmm....
I'll have a clear head tomorrow...
It would be easier if you mark up the number to indicate repetition, something like: "3.[3]" where the part in brackets is the repeating part.
In general:
you have a string: a.b[c]
where a, b, and c all consist only of digits 0-9
This seems over-complicated, but:
The fraction form would be equivalent to the sum of two fractions:
The non-repeating part:
(a*Math.pow(10, Math.ceil(Math.log(b)/Math.log(10)))+b)+" / "+(Math.pow(10, (Math.ceil(Math.log(a)/Math.log(10))+Math.ceil(Math.log(b)/Math.log(10)))))
and the repeating part:
c+" / "+((Math.pow(10, Math.ceil(Math.log(c)/Math.log(10)))-1)*(Math.pow(10, Math.ceil(Math.log(b)/Math.log(10)))));
Example: given 3.4[123]
a=3
b=4
c=123
the result is: (34 / 100) + (123 / 9990)
Of course, then you need something to add the fractions...
Dont follow what you mean but if you can get that to work, great!
The method I always use is (mathematically):
x = 5.142857 142857 142857 ... (given)
1000000x = 5 142 857 . 142857 142857
1000000x - x = 999999x = 5142852
Therefore x = 5142852 / 999999
which is symplifyable to:
x = 36 / 7
but that seems a of work to code... ill see what I can do.
There is always the possibility of itteration, I think that would be the best way to go.
Dont forget to cut off the last digit (if it is repeating) when taking the number as a string becasue that digit is sometimes rounded.