Skip to content

Instantly share code, notes, and snippets.

@seiyria
Created May 30, 2019 20:13
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 seiyria/65d7b9dd24f06a758fcd5f8cdb1a9539 to your computer and use it in GitHub Desktop.
Save seiyria/65d7b9dd24f06a758fcd5f8cdb1a9539 to your computer and use it in GitHub Desktop.
const Random = require('random-js').Random;
const TOTAL_SIMULATIONS = 1;
const MAX_PLAYERS = 100000;
const TOTAL_FRIENDS_PER_PLAYER = 0;
const calculateChoices = () => {
const random = new Random();
const ALL_PLAYERS = Array(MAX_PLAYERS).fill(0).map((x, i) => i);
const PLAYERS_CHOSEN_COUNT = Array(MAX_PLAYERS).fill(0);
const LOANS_PER_DAY_PER_PLAYER = 3;
// only 15 are guaranteed to be on your list
const PLAYERS_VIEWABLE_IN_LIST_LIMIT = 20;
const PLAYER_FRIENDS = [];
const randomNumberBetween = (min, max) => random.integer(min, max);
const randomSliceOfPlayers = (arr, count) => random.sample(arr, count);
const randomPlayerFromSlice = (arr) => random.sample(arr, 1)[0];
const choosePlayer = (playerId) => {
PLAYERS_CHOSEN_COUNT[playerId]++;
};
const makeFriends = () => {
for(let i = 0; i < MAX_PLAYERS; i++) {
PLAYER_FRIENDS[i] = {};
}
if(!TOTAL_FRIENDS_PER_PLAYER) return;
};
makeFriends();
for(let playerId = 0; playerId < MAX_PLAYERS; playerId++) {
for(let loanNum = 0; loanNum < LOANS_PER_DAY_PER_PLAYER; loanNum++) {
let chosenPlayer = -1;
do {
const visibleList = randomSliceOfPlayers(ALL_PLAYERS, PLAYERS_VIEWABLE_IN_LIST_LIMIT);
chosenPlayer = randomPlayerFromSlice(visibleList);
} while(chosenPlayer === playerId);
choosePlayer(chosenPlayer);
}
}
return PLAYERS_CHOSEN_COUNT;
};
const analyze = (arr) => {
let total = 0;
let min = 10000000000;
let max = -1;
arr.forEach(val => {
total += val;
if(val < min) min = val;
if(val > max) max = val;
});
return { min, max, total, avg: Math.floor(total / arr.length) };
};
const analyzeAnalyses = (arr) => {
const total = TOTAL_SIMULATIONS;
let avgMin = 0;
let avgMax = 0;
let avgAvg = 0;
arr.forEach(({ min, max, avg }) => {
avgMin += min;
avgMax += max;
avgAvg += avg;
});
return { endMin: Math.floor(avgMin / arr.length), endMax: Math.floor(avgMax / arr.length), total, endAvg: Math.floor(avgAvg / arr.length) };
};
const allAnalyses = [];
for(let simNum = 0; simNum < TOTAL_SIMULATIONS; simNum++) {
console.log(`Beginning simulation ${simNum + 1}...`);
const res = calculateChoices();
const resAnalysis = analyze(res);
allAnalyses.push(resAnalysis);
if(TOTAL_SIMULATIONS <= 5) {
console.log(`Choice results`, res, resAnalysis);
console.log();
}
}
console.log();
console.log('Final results:', analyzeAnalyses(allAnalyses));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment