title: OpenGL Pixel Shader: how to generate random matrix of 0s and 1s (on each pixel)?
body: <p>So what I need is simple: each time we perform our shader (meaning on each pixel) I need to calculate random matrix of <code>1</code>s and <code>0</code>s with <code>resolution == originalImageResolution</code>. How to do such thing? </p>
  
  <p>As for now I have created one for <a href="http://www.iquilezles.org/apps/shadertoy/" rel="nofollow noreferrer">shadertoy</a> random matrix resolution is set to 15 by 15 here because gpu makes chrome fall often when I try stuff like 200 by 200 while really I need full image resolution size</p>
  
  <pre><code>#ifdef GL_ES
  precision highp float;
  #endif
  
  uniform vec2 resolution;
  uniform float time;
  uniform sampler2D tex0;
  
  float rand(vec2 co){
      return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * (43758.5453+ time));
  }
  vec3 getOne(){
      vec2 p =  gl_FragCoord.xy / resolution.xy;
      vec3 one;
      for(int i=0;i&lt;15;i++){
          for(int j=0;j&lt;15;j++){
              if(rand(p)&lt;=0.5)
                  one = (one.xyz + texture2D(tex0,vec2(j,i)).xyz)/2.0;
          }
      }
      return one;
  }
  
  void main(void)
  {
      gl_FragColor = vec4(getOne(),1.0);
  }
  </code></pre>
  
  <p>And one for Adobe pixel bender:</p>
  
  <pre><code>&lt;languageVersion: 1.0;&gt; 
  
  kernel random
  &lt;   namespace : "Random";
      vendor : "Kabumbus";
      version : 3;
      description : "not as random as needed, not as fast as needed"; &gt;
  {
  
      input image4 src;
      output float4 outputColor;
  
  float rand(float2 co, float2 co2){
      return fract(sin(dot(co.xy ,float2(12.9898,78.233))) * (43758.5453 + (co2.x + co2.y )));
  }
  
  float4 getOne(){
      float4 one;
      float2 r = outCoord();
      for(int i=0;i&lt;200;i++){
          for(int j=0;j&lt;200;j++){
              if(rand(r, float2(i,j))&gt;=1.0)
                  one = (one + sampleLinear(src,float2(j,i)))/2.0;
          }
      }
      return one;
  }
  void
  evaluatePixel()
  {
      float4 oc = getOne();
      outputColor = oc;
  }
  }
  </code></pre>
  
  <p>So my real problem is - my shaders make my GPU deiver fall. How to use GLSL for same purpose that I do now but with out failing and if possible faster? </p>
  
  <p><strong><em>Update:</em></strong>
  What I want to create is called <a href="http://dsp.rice.edu/cscamera" rel="nofollow noreferrer">Single-Pixel Camera</a> (google Compressive Imaging or Compressive Sensing), I want to create gpu based software implementation. </p>
  
  <p>Idea is simple:</p>
  
  <ul>
  <li>we have an image  - <code>NxM</code>.</li>
  <li>for each pixel in image we want GPU to performe the next operations:
  
  <ul>
  <li>to generate <code>NxM</code>matrix of random values - <code>0</code>s and <code>1</code>s.</li>
  <li>compute <a href="http://en.wikipedia.org/wiki/Arithmetic_mean" rel="nofollow noreferrer">arithmetic mean</a> of all pixels on original image whose coordinates correspond to coordinates of <code>1</code>s in our random <code>NxM</code> matrix</li>
  <li>output result of arithmetic mean as pixel color.</li>
  </ul></li>
  </ul>
  
  <p>What I tried to implement in my shaders was simulate that wary process. </p>
  
  <p>What is really stupid in trying to do this on gpu:</p>
  
  <ul>
  <li>Compressive Sensing does not tall us to compute <code>NxM</code> matrix of such arithmetic mean values, it meeds just a peace of it (for example <code>1/3</code>). So I put some pressure I do not need to on GPU. However testing on more data is not always a bad idea.</li>
  </ul>