Random number generation in JavaScript

Generating a random number in JavaScript is simple if you don't care about quality, more complicated if you do. When I say "in JavaScript" I mean both of the JavaScript runtimes - the browsers with their DOM/window, and Node.js without those things - and without all the modern browser APIs.

  1. The default, old-fashioned way to get a random number in JavaScript is by using Math.random(). However, the numbers produced from Math.random() are not secure for crypto applications as they are known to contain predictable patterns. The reason for this non-randomness isn't simply

  2. ...

MEAN vs LAMP stacks defined and compared

The long popular web development stack LAMP is slowly being chipped away from its dominant position by newer, trending technologies, letter by letter. MEAN is one total replacement which has been gaining ground lately.

What do those acronyms mean?

  • MEAN: MongoDB, ExpressJS, AngularJS, Node.js
  • LAMP: Linux, Apache, MySQL, PHP

MongoDB is a NoSQL database which won't support your existing applications because they would require some sort of relational database with SQL querying capability, probably including a whole lot of very custom...

Google Nexus 5X vs Nexus 5 original

I've had the Nexus 5X since it came out. And have still been using my Nexus 5 this whole time. At first I was unsure how I felt about the 5X which I had pre-ordered sight unseen. Now I feel I can give a fuller review and comparison.

  1. I really liked the size of the Nexus 5, the dimensions as well as the weight. The Nexus 5X is actually thinner than the 5, ever so slightly, but significantly taller and noticeably wider when you place the two phones back to back. The 5X also feels bigger in the hand and the difference in length makes it balance differently, sometimes feeling

  2. ...

Querying the database in Drupal 7 using db_select

Drupal, unlike plain old blog software, gives you the ability to create custom content object types with backing storage in the database as custom tables, doing all this without requiring any programming knowledge. When you create a new custom Content Type and add fields to it, in effect, you are creating a new database table for those fields.

Drupal also gives you ways to access and query the data without writing code. The most powerful form of this is Views, which every Drupal site should install as soon as starting. In the Views UI, you can select which fields (database columns...

Python-like List Comprehensions in JavaScript

Python has a neat syntactic feature where you can take a literal list constructor's bracket notation (e.g. [2, 4, 6, 8, 10]) but have code statements generate the list's contents. So instead of writing out the 5 even numbers in the example, one could write [(x+1)*2 for x in range(5)] which turns out to be more characters but you can imagine the savings if the series in the list was longer. Instead of range() we could have a literal list [0, 1, 2, 3, 4] or even a generator function, anything which is iterable.

How could we do this...

List Comprehensions in PHP (how to emulate Python)

Python has a sweet syntactic feature where you can take a literal list written out in bracket notation (e.g. [1, 3, 5, 7, 9]) but have inline expressions generate the list's contents. So instead of writing out the 5 consecutive odd numbers like above, in Python we can say [(x*2+1 for x in range(5)] which turns out to be more characters but you can imagine the savings if the series in the list was longer and not just odd. Instead of range() we could have a literal list [0, 1, 2, 3, 4] or even a generator function, anything which is...

Learn CSS by playing with resources at hand: Chrome Developer Tools

CSS is all around us. It's the invisible language that dictates how every web page, including the one you're reading now, looks - from color to size and shape, layouts and positions of each piece, all that we call "style". CSS stands for Cascading Style Sheets, a stylesheet simply being the text file containing the CSS definitions. You can take a look at a stylesheet now by right-clicking somewhere in a web page and selecting "Inspect" in Chrome or "Inspect element" in Safari. But you should use Chrome for this exercise.


Vim windows and window-splitting

A lifechanging feature of Vim for me was the window-splitting feature. In MacVim, you can even access a tiny slice of this through the Apple menu (File -> Split-Open). So you can view multiple files in other "windows" in the same Vim session. In fact, you can view the same file at different lines in different windows. And you can split windows both vertically (side by side) and horizontally (default). You can split already split...

The Difference between JavaScript and Java

Many non-programmers and young programmers are confused by the names Java and JavaScript. In fact, JavaScript was renamed to JavaScript as an afterthought and a co-marketing campaign together with Sun (the creator of Java). But there are succinct jokes for this naming coincidence: Java and JavaScript are the same like car and carpet, or ham and hamster.

First, the similarities. On a cursory look, some of the syntax of Java and JavaScript code look alike. This is because they're somewhat descended from C (putting them in the Algol family but they are both more similar to C than...

Isomorphic JavaScript myth

Isomorphic JavaScript is the idea of writing JavaScript that can run on the server just as well as in the browser, thus your otherwise frontend-only single page app (SPA), which only makes HTML appear after some post-load requests and execution, can also render the same HTML server-side (which is how things used to work) and give something to search engines to understand. AngularJS required hacks using PhantomJS to achieve this, by running a headless browser on your server.

In theory, a full stack web developer then only needs to know one language - JavaScript. In practice, knowing...

Creating forms with callbacks in React+Redux

In React, you have some simple/dumb components which are just html and other components and maybe render some values which are passed in. It becomes a little trickier when you want to add some functionality to the component, such as form elements - inputs and buttons and submit actions. What I mean by "forms with callbacks" is that you want to execute some JavaScript and also change state in your React app, but do so without giving direct access to state to the component. Instead here's one way to do it.

  1. Draw out the HTML (don't worry about using React components yet) in JSX
  2. ...

Quick explanation of JavaScript Promises

"JavaScript Promises: reason about async events, serially chain them, reduce them. General replacement for XHR"

What is a promise? Conceptually and outside the context of code, a promise is either a willful intention of achieving a result or a promise is considered the actual result itself. EcmaScript 6 (ES6) introduces the Promises API which is currently implemented by most browsers (and Edge but not IE according to caniuse.com - but there's a polyfill for that). So a JavaScript Promise is simply an object of some callback...

Assembly language vs Bytecode vs WebAssembly vs Asm.js


In the beginning there was a CPU. And the CPU could be loaded with data which were numbers and these magical numbers would cause the CPU to process those numbers and produce new numbers. This data which the CPU was running, the numbers fed into the computer, was machine code. And all programming of computers that's still done today ultimately ends up turning into machine code for the CPU, binary machine code being the only thing a computer can understand.

But long series of numbers, where each 256 or more unique numbers has special meaning which changes depending...

Top 5 Programming Languages to Learn in 2016

It's a new year in a world that's moving faster than ever. This means it's important to keep up to date on technology, and young people who would have not been programmers before need to learn to code. There are various lists of programming languages by popularity of demand but they are too broad to read at face value. I've seen many people fail to learn a programming language when they had nothing to apply it to and were simply learning how a language works. It's a bit like learning a foreign language by reading a dictionary. The following list are my choices (not necessarily in order)...

Why Tcl sucks as a programming language

Tcl is a dying language. In the free marketplace of ideas, tcl lacks the community of fans as well as corporate support (see the languages invented at Microsoft, or the huge push in JavaScript from Google's optimization of V8 and thus Node.js as well as Go from Google, or all the large web companies who help PHP dominate most of the software running the web, whereas Tcl's was once sponsored by Sun who then spawned Java and then died/got swallowed by Oracle). Also, tcl sucks.

There was a time (in the 90s) that Unix graphical programs were all universally ugly and many used the Tk...

Developer interview: What is AngularJS and What about Angular 2?

My friend Chau is a long-time Microsoft / .Net developer who has recently switched his company's focus to single-page apps (SPAs), especially web apps built using Angular. He's preparing to use Angular 2.0 now. I asked him to explain Angular a bit.

What is Angular?

A popular (frontend web) UI framework for single page applications (SPA) from Google.

What does it replace? There are a lot of JavaScript frameworks out there nowadays, so why this one?

We started out using Knockout for 2-way binding mostly but it didn't take care of anything outside of that....

What is a pseudo-random number generator (PRNG) and why do they matter?

Recently, a problem was discovered in the pseudo-random number generator which Google Chrome and Node.js's V8 JavaScript engine uses and depends on. The problem was that a pattern was discovered in the series of numbers which the RNG algorithm produced, meaning you could guess the "random" outcome at a probability higher than chance, giving you an advantage - whether the application is breaking encryption or passwords or beating games of chance.

Edward Snowden, who had worked for the CIA, warned of problems or backdoors in hardware random number generators which many computers...

How to login to any website using Curl from the command line or shell script

There are times you need to scrape/crawl some field on a page but the page requires authentication (logging in). Unless the site is using Basic Auth, where you can have the username and password in the url like http://username:[email protected]/ then you'll need to curl with more sophistication. Besides curl, there are other web tools which you can use on the command line such as links/elinks (elinks is an enhanced version of links which also supports JavaScript to a very limited extent). Links and curl will not execute JavaScript though, so if that's necessary to get...

JavaScript's Symbol Type (ECMAScript 6)

JavaScript is not a strongly-typed language like many classic compiled languages. Instead, like many scripting languages, it is loosely typed and has just a few basic types: number, string, object, boolean. There are also two types which are used as values (rather than to hold varying values like numbers and strings) which are null (no value) and undefined (the initial or undefined value of a variable). JavaScript is aware of Arrays as a type of object, but is still an object and not a separate type. Objects in JavaScript take prototypical inheritence. A literal object looks like a Python...

Use an external USB drive across Windows and Mac OSX: exFAT, NTFS, HFS+

Since the beginning of time, Macs and PCs (IBM-compatible / Intel-chip / Microsoft DOS or Windows OS) used the same or similar hardware, including for external storage, but had differences in software which made sharing of hardware between them incompatible. Even as each operating system (from Mac OS up to OSX, DOS to Windows 3.1 to Windows NT) upgraded and used new disk formats, they remained different and incompatible. Other OSs like Linux and the BSDs had their own quirks, although they also sought to be able to work with DOS disks from the beginning, and can mount disks from other...

TECH.SAIGONIST.COM | Tips, tricks, hacks, howtos, & research on web development, servers, and software



Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /home/tomo/web/tech.saigonist.com/drupal7/php/includes/common.inc:2776) in drupal_send_headers() (line 1486 of /home/tomo/web/tech.saigonist.com/drupal7/php/includes/bootstrap.inc).
  • Error: Class 'CToolsCssCache' not found in _cache_get_object() (line 32 of /home/tomo/web/tech.saigonist.com/drupal7/php/includes/cache.inc).
The website encountered an unexpected error. Please try again later.