Skip to content

Commit ef336d3

Browse files
author
Nicholas Thomson
committed
Add exponential distribution for better-looking data
1 parent 2224119 commit ef336d3

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

app/scripts/lib/webApiFaker.js

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
'use strict';
22

3-
var rnd_bmt = function() {
4-
var x = 0, y = 0, rds, c;
3+
var normal_distribution = function() {
4+
return (Math.random()*2-1)+(Math.random()*2-1)+(Math.random()*2-1);
5+
};
6+
var exponential_distribution = function(min, max) {
7+
var increment = (max - min) / 6;
8+
var num;
59
do {
6-
x = Math.random()*2-1;
7-
y = Math.random()*2-1;
8-
rds = x*x + y*y;
10+
var u = Math.random();
11+
var t = (-1 * Math.log(u))/1;
12+
num = min + (t * increment);
913
}
10-
while (rds === 0 || rds > 1);
11-
c = Math.sqrt(-2*Math.log(rds)/rds);
12-
return [x*c, y*c];
14+
while(num <= min || num >= max);
15+
return Math.floor(num);
16+
};
17+
var distributed_random = function(min, max) {
18+
var mean = (min + max)/2;
19+
var deviation = mean/3;
20+
var num;
21+
do {
22+
num = Math.floor((normal_distribution() * deviation) + mean);
23+
}
24+
while(num <= min || num >= max);
25+
return num;
1326
};
14-
15-
var distributed_random = function(min, max) { return Math.floor(rnd_bmt()[0] * (max - min) + min); };
1627

1728

1829
(function (ng, fk) {
@@ -71,7 +82,7 @@ var distributed_random = function(min, max) { return Math.floor(rnd_bmt()[0] * (
7182
id = id || 0;
7283
return {
7384
id: id,
74-
name: fk.random.bs_buzz(),
85+
name: fk.random.bs_noun(),
7586
permalink: name.toLowerCase(),
7687
company_ids: [],
7788
investor_ids: []
@@ -102,12 +113,14 @@ var distributed_random = function(min, max) { return Math.floor(rnd_bmt()[0] * (
102113
*/
103114
var linkGeneratedLists = function(companies, investors, categories) {
104115
_.each(companies, function(company){
105-
var category = categories[Math.floor(Math.random()*categories.length)];
116+
var cat_id = exponential_distribution(0, categories.length);
117+
console.log(cat_id);
118+
var category = categories[cat_id];
106119
company.category_id = category.id;
107120
category.company_ids.push(company.id);
108121

109-
_(Math.floor(Math.random() * (1) + 24)).times(function(){
110-
var investor = investors[Math.floor(Math.random()*investors.length)];
122+
_(exponential_distribution(1, 10)).times(function(){
123+
var investor = investors[exponential_distribution(0, investors.length)];
111124
company.investor_ids.push(investor.id);
112125
category.investor_ids.push(investor.id);
113126
investor.invested_company_ids.push(company.id);
@@ -116,10 +129,13 @@ var distributed_random = function(min, max) { return Math.floor(rnd_bmt()[0] * (
116129
});
117130
};
118131

132+
/**
133+
* Initiate and respond with "fake" backend data instead of querying an actual API
134+
*/
119135
var setupStubbedBackend = function() {
120-
var categories = generateDataList(10, randomCategory);
121-
var investors = generateDataList(10000, randomInvestor);
122-
var companies = generateDataList(15000, randomCompany);
136+
var categories = generateDataList(42, randomCategory);
137+
var investors = generateDataList(9987, randomInvestor);
138+
var companies = generateDataList(16635, randomCompany);
123139
linkGeneratedLists(companies, investors, categories);
124140

125141
ng.module('crunchinatorApp')

app/views/main.tpl.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<div class="container" ng-controller="CrunchinatorCtrl">
2-
<h4>Environment: {{environment}}</h4>
32
<div class="row">
43

54
<div class='col-sm-6 spacing-med'>

0 commit comments

Comments
 (0)