Skip to content

Instantly share code, notes, and snippets.

@mamemomonga
Last active December 20, 2015 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mamemomonga/6090387 to your computer and use it in GitHub Desktop.
Save mamemomonga/6090387 to your computer and use it in GitHub Desktop.
ExponentialEndlessSummer.js for Reblog Machine
// vim:set fenc=utf-8 ff=unix ft=javascript:
/**
* @preserve
* ExponentialEndlessSummer.js for Reblog Machine
* https://gist.github.com/mamemomonga/6090387
* from "Exponential Endless Summer 2014" by saitamanodoruji
* http://tumblr.g.hatena.ne.jp/saitamanodoruji/20140831/1409496698
*/
'use strict';
(function() {
var ExponentialEndlessSummer=function() {
this.params = [
// f_i(t) = Alpha_i * exp[Beta_i * (t - t_0)]
// t: Unix time [msec]
// t_0 = 1136073600000 [msec] (2006-01-01T00:00:00+09:00)
{coefAlpha: 1.211 * Math.pow(10, -1), coefBeta: 1.571 * Math.pow(10, -10), upperBound: 1160012789359},
{coefAlpha: 3.619 * Math.pow(10, -13), coefBeta: 1.266 * Math.pow(10, -9), upperBound: 1163269541924},
{coefAlpha: 2.120 * Math.pow(10, 0), coefBeta: 1.846 * Math.pow(10, -10), upperBound: 1172120791893},
{coefAlpha: 1.179 * Math.pow(10, -49), coefBeta: 3.331 * Math.pow(10, -9), upperBound: 1173392561265},
{coefAlpha: 4.702 * Math.pow(10, -7), coefBeta: 7.024 * Math.pow(10, -10), upperBound: 1176405323219},
{coefAlpha: 5.338 * Math.pow(10, 1), coefBeta: 2.425 * Math.pow(10, -10), upperBound: 1182930300850},
{coefAlpha: 8.255 * Math.pow(10, 3), coefBeta: 1.349 * Math.pow(10, -10), upperBound: 1193299359828},
{coefAlpha: 2.496 * Math.pow(10, 6), coefBeta: 3.509 * Math.pow(10, -11), upperBound: 1241622616806},
{coefAlpha: 2.907 * Math.pow(10, 5), coefBeta: 5.546 * Math.pow(10, -11), upperBound: 1283088065116},
{coefAlpha: 5.206 * Math.pow(10, 4), coefBeta: 6.716 * Math.pow(10, -11), upperBound: 1291075200000},
{coefAlpha: 8.329 * Math.pow(10, 4), coefBeta: 6.521 * Math.pow(10, -11), upperBound: 1292241600000},
{coefAlpha: 8.396 * Math.pow(10, 4), coefBeta: 6.541 * Math.pow(10, -11), upperBound: 1307576237376},
{coefAlpha: 7.746 * Math.pow(10, 4), coefBeta: 6.588 * Math.pow(10, -11), upperBound: 1313586050187},
{coefAlpha: 8.798 * Math.pow(10, 6), coefBeta: 3.922 * Math.pow(10, -11), upperBound: 1340572542928},
{coefAlpha: 2.693 * Math.pow(10, 8), coefBeta: 2.249 * Math.pow(10, -11), upperBound: 1373049162025},
{coefAlpha: 1.225 * Math.pow(10, 9), coefBeta: 1.610 * Math.pow(10, -11), upperBound: Number.POSITIVE_INFINITY},
];
}
ExponentialEndlessSummer.prototype={
draw: function(oldestID, newestID) {
var oldestUTime = this.getUnixTimeFromPostID(oldestID);
var newestUTime = this.getUnixTimeFromPostID(newestID);
var drawnUTime = oldestUTime + Math.floor((newestUTime - oldestUTime) * Math.random());
return this.getPostIDFromUnixTime(drawnUTime);
},
getPostIDFromUnixTime: function(utime) {
if ( utime < 1149465600000 ) {
return;
} else if ( utime === Number.POSITIVE_INFINITY ) {
return Number.POSITIVE_INFINITY;
}
for (var i = 0; i < this.params.length; i++) {
if ( utime <= this.params[i].upperBound ) {
return Math.ceil(this.params[i].coefAlpha * Math.exp(this.params[i].coefBeta * (utime - 1136073600000)));
}
}
},
getUnixTimeFromPostID: function(postID) {
if ( postID < 1 ) { return; }
for (var i = 0; i < this.params.length; i++) {
if ( postID <= this.getPostIDFromUnixTime(this.params[i].upperBound) ) {
return Math.floor(Math.log(postID/this.params[i].coefAlpha)/this.params[i].coefBeta) + 1136073600000;
}
}
}
};
window['ExponentialEndlessSummer']=ExponentialEndlessSummer;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment