Navigation About Mode >LTR>

Tweet Random

A twitter tweet base pseudo random functions, that provides repeatability of the functions with the same result.
This function can also be used with other text-based random sources like the source of the performed code.

tweet random code example

An experiment to create random like numbers out of static structured text and data values, as for instance a single tweet. A tweet has several public available countable values, which are the followers, friends, listed (how much does the user appear in a list), favourites and the statuses(posts).
I myself decided to try a second implementation of selected portions of texts. Each text should have an author, the author has a birthday, a name, the text might have an title and should have a publishing date. For dates I use a mySQL like representation thought without dashes. that is YEARMonthDay or YYYYMMDD. Part of the randomness should be the source code of the generator. So there is a little self-referential-ness involved. With this one can create a reproducible somewhen ending aka repeating stream of images.

The important methods are getTweetRandomData and tweetRandom.
getTweetRandomData generates the initial dataset, tweetRandom delivers numbers between 0 and 1.

function getTweetRandomData() {
  const authorname = "malte kosian";
  const authoryearofbirth = 19650109;
  const textpublishedat = 20191025;
  const title = "a title test";
  const text =
    "A text of a certain length. But we can not take javascript date values here or in the numbers or n the dnums arrays. The values must be fixed, textspecific and reproduceable somehow.";
  //const script = document.querySelector('script');
  function dumpObject(obj) {
    var output = "";
    for (var i in obj) {
      var msg = i + "= " + obj[i];
      output += msg + "\n";
    }
    return output;
  }

  console.log(dumpObject(generator));
  const data =
    authorname +
    authoryearofbirth +
    title +
    textpublishedat +
    text +
    dumpObject(generator); //script.innerText
  dnums = [];
  for (let c of data) {
    dnums.push(c.charCodeAt(0));
  }
  //an array of 5 modificator numbers
  numbers = [
    Math.round(authoryearofbirth / 5),
    Math.round(textpublishedat / 10),
    authorname.length,
    title.length,
    text.length - 1,
  ];
  console.log(numbers);
  return { numbers: numbers, chars: dnums, index: 0, pri: 0, currentamount: 0 };
}

function tweetRandom(data) {
  data.currentamount =
    data.currentamount +
    data.chars[data.pri] +
    data.numbers[data.pri % data.numbers.length];
  data.pri++;
  data.pri = data.pri % data.chars.length;
  data.currentamount = data.currentamount % 256;
  return data.currentamount / 255;
}

generator = {
  draw: function (data) {
    const canvas = document.createElement("canvas");
    canvas.width = canvas.height = 512;
    const ctx = canvas.getContext("2d");
    ctx.fillStyle =
      "rgba(" +
      Math.floor(tweetRandom(data) * 4) * 85 +
      "," +
      Math.floor(tweetRandom(data) * 4) * 51 +
      "," +
      Math.floor(tweetRandom(data) * 4) * 51 +
      ",0.5)";
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    const max = Math.ceil(tweetRandom(data) * 100);
    for (let i = 0; i < max; i++) {
      ctx.fillStyle =
        "rgba(" +
        Math.floor(tweetRandom(data) * 4) * 85 +
        "," +
        Math.floor(tweetRandom(data) * 4) * 51 +
        "," +
        Math.floor(tweetRandom(data) * 4) * 51 +
        ",0.5)";
      const x = Math.floor(tweetRandom(data) * 512);
      const y = Math.floor(tweetRandom(data) * 512);
      const w = Math.floor(tweetRandom(data) * (512 - x));
      const h = Math.floor(tweetRandom(data) * (512 - x));
      ctx.fillRect(x, y, w, h);
    }
    return canvas;
  },
};

function generateAndTweet(tweet, i, data) {
  const canvas = document.createElement("canvas");
  canvas.width = canvas.height = 512;
  const ctx = canvas.getContext("2d");
  //tweet = data;
  //create palette be tweet data
  //create numbers
  /**/
  if (data == null) {
    /**/
    data = getTweetRandomData();
  }
  data.index = i;
  //console.log('data->', i, data.index);
  ctx.clearRect(0, 0, canvas.width, canvas.height);

  ctx.drawImage(generator.draw(data), 0, 0, canvas.width, canvas.height);
  /**/
  if (i < 129) {
    //console.log('tweet.id_str', tweet.id_str, i);
    document.body.appendChild(canvas);
    i++;
    //out.on('finish', () => generateAndTweet(tweet, i, data));
    generateAndTweet(tweet, i, data);
  } else {
    //out.on('finish', () => drawPreview(outpath, tweet));
    console.log("finish");
  }
}

generateAndTweet(null, 0, null);
Formal In Formal Navigation