Automatic type coercion sucks!

By trying to make your life simpler, JavaScript makes your life really hard. This snippet meant one hour of debugging:

          var w = canvas.getAttribute('width');
          var neighb = [ -1, 1, w, -w ];

Looks innocent enough, n’est-ce pas? Unfortunately, canvas.getAttribute() returns a string, not an integer. So far, so good. If JavaScript wouldn’t do type coercion, I would have noticed this right away, cursed for two seconds, and moved on. But no, strings become integers by magic, then merrily convert back to strings, and so on. Freakingly, -w becomes an integer, w stays a string. Therefore, my implementation of the Leath algorithm could grow clusters west, east, and north (-1, 1, -w), but not south (w).

This made my life miserable for at least an hour. I now firmly declare that automatic type coercion is a really bad idea. Unfortunately, I have planned to have to deal a lot with JavaScript in the future. Oh ye goods, have ye no mercy? Well, next thing is getting the Venkman debugger.

Oh, and by the way, one more gripe with JavaScript: the built-in random number generator (Math.random()) does not support explicitly setting the seed, but instead always uses the current time as seed. What the …? Well, heed the advice of a Monte Carlo simulant: always implement your own random number generator, otherwise you don’t now what you get.

As a service, the very simple (and obsolete) MINSTD generator in JavaScript:

var seed = 1; var a = 16807; var m = 2147483647;
function initrand(ini) {
  seed = ini;
  if (seed < 1) seed = 1;
  for(var i=0; i<3; i++) myrand();
function myrand() {
  seed = (seed * a) % m;
  return seed / m;

But be aware that this generator is very outdated! It's described in S. K. Park and K. W. Miller, "Random Number Generators: Good ones are hard to find", Communications of the ACM, October 1988, Volume 31, Issue 10, pages 1192–1201. Never use it for any serious work, only for toy problems (you can't do much more with JavaScript, anyway). I was just to lazy to hack anything more in.

About Daniel Tiggemann

Software-developer living in Cologne, Germany. Was once a physicist, specialized in computer simulations and parallel programming. Now more into JavaScript, web frontend development, and especially mobile computing.
This entry was posted in Programming, Randomness. Bookmark the permalink.

629 Responses to Automatic type coercion sucks!

  1. Pingback: Hong Kong Serviced Apartments

  2. Pingback: Facebook chat

  3. Pingback: Dallas Texas photographers

  4. Pingback: A1 Chicago Limo Service Airport ORD O'Hare Limo

  5. Pingback: water slides Long Island

  6. Pingback: Christianity and Politics

  7. Pingback: tadawul fx

  8. Pingback: musicas para ouvir

  9. Pingback: how to make money


  11. Pingback: blogging tips

  12. Pingback: how to make money from the internet

  13. Pingback: Reptilien kaufen

  14. Pingback: how to

  15. Pingback: cialis

  16. Pingback: viagra

  17. Pingback: viagra

  18. Pingback: buy cialis

  19. Pingback: Cialis

  20. Pingback: valium

  21. Pingback: viagra

  22. Pingback: cheap viagra

  23. Pingback: cialis

  24. Pingback: klonopin

  25. Pingback: adipex

  26. Pingback: viagra online without prescription

  27. Pingback: levitra

  28. Pingback: buy cialis

  29. Pingback: cheap cialis