# Thread: Here are 2 prototype-functions I created.

1. 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)]})
}```

2. Artarmon Zealand
Join Date
Feb 2005
Location
USA
Posts
1,534
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}`
Last edited by BigMoosie; 08-26-2005 at 04:25 AM.

3. Artarmon Zealand
Join Date
Feb 2005
Location
USA
Posts
1,534
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.

4. Geez, those results are shocking -- 1324 and 3124 got as high as x3.
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.

5. Artarmon Zealand
Join Date
Feb 2005
Location
USA
Posts
1,534
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]);
}
);```

6. 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!
Last edited by HaganeNoKokoro; 06-18-2005 at 02:45 AM.

7. Artarmon Zealand
Join Date
Feb 2005
Location
USA
Posts
1,534
Thats pretty awesome Hagane, though I dont see the purpose of making it a prototype function.

8. So I could post it in this thread

9. Originally Posted by HaganeNoKokoro
So I could post it in this thread
LOL,
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?
Last edited by Ultimater; 06-19-2005 at 12:55 AM.

10. 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.

11. Artarmon Zealand
Join Date
Feb 2005
Location
USA
Posts
1,534
Does anybody have a function that would turn:

3.3333333333

into

[10,3]

as in 10/3, and the same for any other rational?

12. 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.

13. 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...
Last edited by Ultimater; 06-19-2005 at 04:13 AM.

14. 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...

15. Artarmon Zealand
Join Date
Feb 2005
Location
USA
Posts
1,534
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.

#### Thread Information

##### Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

"

"

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.16270 seconds
• Memory Usage 3,047KB
• Queries Executed 15 (?)
More Information
Template Usage (37):
• (1)SHOWTHREAD
• (1)ad_footer_end
• (1)ad_footer_start
• (1)ad_global_above_footer
• (1)ad_global_below_navbar
• (1)ad_global_header1
• (1)ad_global_header2
• (1)ad_navbar_below
• (1)ad_showthread_firstpost_sig
• (1)ad_showthread_firstpost_start
• (1)ad_thread_first_post_content
• (1)ad_thread_last_post_content
• (3)bbcode_code
• (1)bbcode_quote
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (1)header
• (1)headinclude
• (1)headinclude_bottom
• (15)memberaction_dropdown
• (1)navbar
• (4)navbar_link
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (1)pagenav
• (1)pagenav_curpage
• (4)pagenav_pagelink
• (15)postbit
• (15)postbit_onlinestatus
• (15)postbit_wrapper
• (1)spacer_close
• (1)spacer_open
• (1)tagbit_wrapper

Phrase Groups Available (6):
• global
• inlinemod
• postbit
• posting
• reputationlevel
• showthread
Included Files (26):
• ./showthread.php
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/functions_navigation.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_facebook.php
• ./includes/functions_calendar.php
• ./includes/functions_bigthree.php
• ./includes/class_postbit.php
• ./includes/class_bbcode.php
• ./includes/functions_reputation.php
• ./includes/functions_notice.php
• ./packages/vbattach/attach.php
• ./vb/types.php
• ./vb/cache.php
• ./vb/cache/db.php
• ./vb/cache/observer/db.php
• ./vb/cache/observer.php

Hooks Called (74):
• init_startup
• friendlyurl_resolve_class
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_postinfo_query
• fetch_postinfo
• fetch_threadinfo_query
• fetch_threadinfo
• fetch_foruminfo
• load_show_variables
• load_forum_show_variables
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• showthread_start
• showthread_getinfo
• strip_bbcode
• friendlyurl_clean_fragment
• friendlyurl_geturl
• forumjump
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates
• fetch_musername
• notices_check_start
• notices_noticebit
• process_templates_complete
• friendlyurl_redirect_canonical
• showthread_post_start
• showthread_query_postids
• showthread_query
• bbcode_fetch_tags
• bbcode_create
• showthread_postbit_create
• postbit_factory
• postbit_display_start
• bbcode_parse_start
• postbit_imicons
• bbcode_parse_complete_precache
• bbcode_parse_complete
• postbit_display_complete
• memberaction_dropdown
• pagenav_page
• pagenav_complete
• tag_fetchbit_complete
• forumrules
• navbits
• navbits_complete
• build_navigation_data
• build_navigation_array
• check_navigation_permission
• process_navigation_links_start
• process_navigation_links_complete
• set_navigation_menu_element
• build_navigation_menudata
• build_navigation_listdata
• build_navigation_list
• set_navigation_tab_main
• set_navigation_tab_fallback
• navigation_tab_complete
• fb_like_button
• showthread_complete
• page_templates