MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / GenerateRandomIntsWithSum

Function GenerateRandomIntsWithSum

tensorflow/core/kernels/split_v_op_test.cc:35–55  ·  view source on GitHub ↗

Generate "count" random positive integers (not including zero) with sum "sum". Technique based on one from https://math.stackexchange.com/a/1276225 but simplified (especially for zero-based indexing).

Source from the content-addressed store, hash-verified

33// "sum". Technique based on one from https://math.stackexchange.com/a/1276225
34// but simplified (especially for zero-based indexing).
35static std::vector<int64> GenerateRandomIntsWithSum(int64 sum, int count) {
36 CHECK_GE(count, 1);
37 CHECK_GE(sum, count);
38 std::vector<int64> temp(count);
39 for (int i = 0; i + 1 < count; ++i) {
40 temp[i] = lrand48() % (sum - count);
41 }
42 temp[count - 1] = sum - count;
43 std::sort(temp.begin(), std::prev(temp.end()));
44 std::vector<int64> result(count);
45 std::adjacent_difference(temp.begin(), temp.end(), result.begin());
46 for (int i = 0; i < count; ++i) {
47 ++result[i];
48 }
49 CHECK(std::all_of(result.begin(), result.end(),
50 [sum](int64 x) { return x >= 1 && x <= sum; }));
51 CHECK_EQ(std::accumulate(result.begin(), result.end(), static_cast<int64>(0)),
52 sum);
53 CHECK_EQ(result.size(), count);
54 return result;
55}
56
57static Graph* MakeGraph(int split_dim, const std::vector<int64>& size_splits,
58 std::initializer_list<int64> total_size) {

Callers 1

split_v_op_test.ccFile · 0.85

Calls 4

sortFunction · 0.85
beginMethod · 0.45
endMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected