Random Negative or Positive

for (var i:int = 0; i<10; i++){
  // randomly set a variable to -1 or positive 1
  trace(int(Math.random()*2) - 1 | 1);
}

/* outputs something like this
1
1
-1
1
-1
-1
1
1
-1
-1
*/

The past few days I found myself using this one-liner a few times. Just a quick way to randomly get -1 or 1.

I was brainstorming today and wrote a weird/bad inline version of an old post:

var rand:Number = [-3, 2, 1, 1, 1, 0.5][int(Math.random() * 6)];

I need to install a related posts plugin for WordPress – would be nice if this post were related to my old post about Tausworthe random seeds.

Been posting medium/large snippets and snippets the relate to QuickBox2D – so today I figured I’d get back to basics…

3 comments

Leave a Reply

Your email address will not be published. Required fields are marked *