Online quantum random number generator

edited July 2017 in Share Your Work

I'm sure someone who knows more about json can write this code better: (Also I haven't checked this code too well!!!)

// qrng.anu.edu.au

QRND qrnd=new QRND();

void setup() {
  noLoop();
  for (int i=0; i<1024; i++){
    int r=qrnd.nextInt();
    println(r);
    println(hex(r));
    println(binary(r));
  }
}

class QRND {
  int pos;
  int[] rData=new int[512];

  int nextInt() {
    if (pos==0) {
      JSONObject json=loadJSONObject("https://" + "qrng.anu.edu.au/API/jsonI.php?length=1024&type=uint16");
      String[] tok=splitTokens(json.toString(), "[,");
      for (int i=0; i<512; i++) {
        rData[i]=(int(trim(tok[i*2+1]))<<16) | int(trim(tok[i*2+2]));
      }
    }
    int r=rData[pos];
    pos=(pos+1) & 511;
    return r;
  }
}

Comments

Sign In or Register to comment.