summaryrefslogtreecommitdiff
path: root/jstests/aggregation/expressions/rand.js
blob: d4d3559bc1adf0518b8c98ad0ecd84aa8de897e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
 * Test the $rand expression.
 */
(function() {
"use strict";

load("jstests/libs/analyze_plan.js");  // For getAggPlanStage().

const coll = db.expression_rand;
coll.drop();

print("Generating test collection...");
const N = 3000;
let i;
const bulk = coll.initializeUnorderedBulkOp();
for (i = 0; i < N; i++) {
    bulk.insert({_id: i, v: 0});
}
assert.commandWorked(bulk.execute());

const randPipeline = [{$project: {r: {$rand: {}}}}, {$group: {_id: 0, avg: {$avg: "$r"}}}];
const resultArray = coll.aggregate(randPipeline).toArray();
assert.eq(1, resultArray.length);
const avg = resultArray[0]["avg"];

print("Average: ", avg);
// For continuous uniform distribution [0.0, 1.0] the variance is 1/12 .
// Test certainty within 10 standard deviations.
const err = 10.0 / Math.sqrt(12.0 * N);
assert.lte(0.5 - err, avg);
assert.gte(0.5 + err, avg);
}());