| 144 | } // namespace |
| 145 | |
| 146 | double PrngTest::UniformChiSquared(int32 range_size, int32 expected_count, |
| 147 | int64 seed) { |
| 148 | int32 sample_size = range_size * expected_count; |
| 149 | |
| 150 | XlaBuilder builder(TestName()); |
| 151 | RngUniform(ConstantR0<int32>(&builder, 0), |
| 152 | ConstantR0<int32>(&builder, range_size), |
| 153 | ShapeUtil::MakeShape(S32, {sample_size})); |
| 154 | |
| 155 | SetSeed(seed); |
| 156 | auto actual = |
| 157 | ExecuteAndTransfer(&builder, /*arguments=*/{}).ConsumeValueOrDie(); |
| 158 | std::vector<int32> counts(range_size, 0); |
| 159 | actual.EachCell<int32>( |
| 160 | [&counts](absl::Span<const int64>, int32 value) { ++counts[value]; }); |
| 161 | int64 sum = 0; |
| 162 | for (int32 i = 0; i < range_size; ++i) { |
| 163 | sum += Square(static_cast<int64>(counts[i] - expected_count)); |
| 164 | } |
| 165 | return static_cast<double>(sum) / expected_count; |
| 166 | } |
| 167 | |
| 168 | // We only test distribution of uniform discrete PRNG as other types are based |
| 169 | // on it. |
nothing calls this directly
no test coverage detected