Code of the Day: Javascript Random Number Generator


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: Undefined array key "layout" in /home/bateeqjg/public_html/news/wp-content/plugins/wp-about-author/wp-about-author.php on line 94

Code
*Updated*

1
2
3
4
5
6
7
8
9
10
//@function: getRandomNum( i, decLen )
/**
* @param i, a number for the number range. [0, i)
* @param decLen, the number of fixed decimal length.
* @returns number that is either a float( number with decimals ) or integer (whole number).
* @note if not arguments are passed, then 0 or 1 is returned.
*/
var getRandomNum = function( i, decLen ){
	return (Math.random() * (i||1) ).toFixed(decLen);
};

Demo
http://jsbin.com/uvuze3/2/

Parameters
Todays code of the day is a random number generator coded in javascript.
getRandomNum() enhances the Math.random() function that javascript provides by offering you the ability to get a random float ( number with decimals ) or integer( whole number ) value.

Example output

1
2
3
4
5
6
//get value from Math.random()
getRandomNum();	//might return 0
//get value between [0,1) with 5 decimals
getRandomNum(1,5);	//might return 0.83034
//get value from [0,99]
getRandomNum(100);	//might return 84

Please leave a comment below.

Larry Battle

Larry Battle

I love to program, and discover new tech. Check out my <a href="http://stackoverflow.com/users/527776/larry-battle">stackoverflow</a> and <a href="https://github.com/LarryBattle">github</a> accounts.

More Posts - Website

Follow Me:Add me on XAdd me on LinkedInAdd me on YouTube

Find equations within chaos.


Warning: Undefined array key "layout" in /home/bateeqjg/public_html/news/wp-content/plugins/wp-about-author/wp-about-author.php on line 94

While I was reading my weekly edition of New Scientist, I found an article that was truly amazing.
In 2009, two Phd scientist at Cornell University developed software that could generate an accurate formula describing an unknown dataset using evolutionary programming as their main tactic. In other words, this program allows you to put in numerical values and you get back an equation that describe it.
The best part of the whole thing is that they released Eureqa (the equation making software) free online for all to download. But… no source code is provided.

For those who don’t know, I once tried to make similar software that use an evolutionary approach to help web designers to craft the prefect CSS layout for their website. However, that didn’t come out too well because of my relative inexperience in the field evolutionary programming and problems associated optimization. But I tried and you can check it out here. Random CSS Generator with Evolution
Well enough of me talking, take a look at the video below to see how Eureqa works.


Try Eureqa and tell me what you think!
Eureqa download (0.83 beta)



Larry Battle

Larry Battle

I love to program, and discover new tech. Check out my <a href="http://stackoverflow.com/users/527776/larry-battle">stackoverflow</a> and <a href="https://github.com/LarryBattle">github</a> accounts.

More Posts - Website

Follow Me:Add me on XAdd me on LinkedInAdd me on YouTube

“Beginning MySql” – Book Review


Warning: Undefined array key "layout" in /home/bateeqjg/public_html/news/wp-content/plugins/wp-about-author/wp-about-author.php on line 94

Here’s a book review I did on amazon.com.
Check out more of my reviews at my amazon.com profile page.

Overall, I love this book, because the authors build your knowledge of databases from the ground up.

Before I got my hands on this book, I knew only a limited amount about databases. Now, I understand advance concepts (like foreign keys, transactions and optimization) and can design, implement and maintain my own simple relational database management system.

This is a thick book that took me about 2 weeks to finish and it would have taken even longer if I decided to follow through with all the “Try it out” sections, exercises and useful examples.

“Beginning MySql” covers the majority of MySql 4.1 by using the definitions of syntax and options as their main teaching tool throughout most of the chapters. This makes MySql easy to learn since you can reference the definitions as a cheat sheet when you’re implementing a database design.

One thing that I noticed is that they need more proofreading. Every now and then, you’ll encounter a typo or wrong information but this is expected since the authors warned of this in the introduction.
Example, page 299: “However, bitwise operations support calculations up to 64 buts”.

The best part of the book, is appendix C. It briefly covers MySql 5 but they introduce you to Triggers, Views and Procedures while updating you on MySql new standards. For exmaple, MySql 5 supports foreign keys for all the table engines, not just for INNODB.
If you’re the type that has a hard time reading documentation online, then I would strongly recommend this book. Otherwise you might be able to learn MySql using Youtube and the MySql homepage.

Note: Since this book only converts version 4.1 of MySql, you need to checkout the on-line documentation for changes in MySQL 5.

You can buy the book for less than $10. Link below.

Larry Battle

Larry Battle

I love to program, and discover new tech. Check out my <a href="http://stackoverflow.com/users/527776/larry-battle">stackoverflow</a> and <a href="https://github.com/LarryBattle">github</a> accounts.

More Posts - Website

Follow Me:Add me on XAdd me on LinkedInAdd me on YouTube

Javascript Code of the Day: get highlighted text


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: Undefined array key "layout" in /home/bateeqjg/public_html/news/wp-content/plugins/wp-about-author/wp-about-author.php on line 94

Discovered the code below while browsing online. Inspired from codetoad.com.

1
2
3
4
5
6
function getHighlightedText(){
     return ((window.getSelection) ? window.getSelection() 
          : (document.getSelection) ? document.getSelection()
          : (document.selection) ? document.selection.createRange().text 
          : null);
}

Usage

1
getHighlightedText();   // returns highlighted material as a string.
Larry Battle

Larry Battle

I love to program, and discover new tech. Check out my <a href="http://stackoverflow.com/users/527776/larry-battle">stackoverflow</a> and <a href="https://github.com/LarryBattle">github</a> accounts.

More Posts - Website

Follow Me:Add me on XAdd me on LinkedInAdd me on YouTube