Page 1 of 1

Faster random binary number generator

Posted: May 5th, 2007, 11:23 am
by 3deyes
I have seen various tutorials for gererating random binary numbers including one on Creativecow.net by Dan Ebberts "random hex grid"
http://www.creativecow.net/articles/ebb ... ndex2.html
The problem is speed if you want any more than a few lines of text. Of course they will all play back at 29.97 fps after the preview has been compiled but during that process they are extremely slow.The Creative cow tutorial ran at 1.0 fps and my system has dual 2.0ghz Opterons with 2Gb ram and the O.S on two Seagate drives in raid 0 with two more of the same for playback.I have read about flipped loops with optimized reverse counting and various tutorials on using a StringBuffer function and using switch instead of if statements.I have come up with the following but it still only runs at 1.5 fps. :( The final version even has unnecessary tabs and spaces removed. I still need to make it hold for 5 frames maybe that will speed it up. How can I do that?
Or maybe a different approach by using a varation of this tutorial. http://aefreemart.com/2006/09/13/ae-captions/

Code: Select all

function get_random() {
 var rn=Math.floor(random()*2);
 return rn;
}
function StringBuffer() { 
   this.buffer = []; 
}
 StringBuffer.prototype.append = function append(string) { 
   this.buffer.push(string); 
   return this; 
}
 StringBuffer.prototype.toString = function toString() { 
   return this.buffer.join(""); 
 } 
var j=("\r"),i=1559,buf=new StringBuffer();
do
{
switch (true)
{
case (i%130==0):buf.append(j);break;
default:buf.append(get_random());
}
}
while (--i)
text.sourceText=buf.toString();
[/url]

Posted: May 6th, 2007, 8:47 pm
by nab
I'm not sure it's faster but you could try the following source text expression:

Code: Select all

numRows = 10;
numCols = 30;
str = "";

for (i = 0; i < numRows; i++) {
   for (j = 0; j < numCols; j++) {
      str += Math.round(random());
   }
   str += "\r";
}  
str;

Posted: May 7th, 2007, 1:43 pm
by 3deyes
Ran at 1.3 Fps. For loops and the + operater for string conjugation are just too slow. But thanks anyway.I think what's needed is a faster lower level language. I wish Adobe would take note of how Autodesk added support for Phyton in Maya 8.5. Anyway, do you know how I make it hold for 5 frames?

Posted: May 7th, 2007, 2:48 pm
by nab
how I make it hold for 5 frames
The easiest way is probably to use posterizeTime() (at the beginning of the code).
In a 25fps comp:

Code: Select all

posterizeTime(5);

Posted: May 7th, 2007, 4:28 pm
by 3deyes
Yes, posterizeTime(5); works thank you. Still the same 1.5 fps though.

Posted: May 8th, 2007, 8:24 am
by Dan Ebberts
This generates a 128x12 random binary block that seems to render quite a bit faster than other methods:

Code: Select all

s = "";
for (j = 1; j <= 12; j++){
  for (i = 1; i <= 4; i++) s += (4294967296 + Math.floor(random(4294967296))).toString(2).substr(1);
  s += "\r";
}
Dan

Posted: May 8th, 2007, 1:09 pm
by 3deyes
It ran a little faster, 1.6 fps. Interesting code though thanks.