// Copyright (c) the JPEG XL Project Authors. All rights reserved. // // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file.
// Random number generator + distributions. // We don't use <random> because the implementation (and thus results) differs // between libstdc++ and libc++.
// Uniformly distributed int64_t in [begin, end), under the assumption that // `end-begin` is significantly smaller than 1<<64, otherwise there is some // bias.
int64_t UniformI(int64_t begin, int64_t end) {
JXL_DASSERT(end > begin); returnstatic_cast<int64_t>((*this)() % static_cast<uint64_t>(end - begin)) +
begin;
}
// Same as UniformI, but for uint64_t.
uint64_t UniformU(uint64_t begin, uint64_t end) {
JXL_DASSERT(end > begin); return (*this)() % (end - begin) + begin;
}
// Uniformly distributed float in [begin, end) range. Note: only 23 bits of // randomness. float UniformF(float begin, float end) { float f; // Bits of a random [1, 2) float.
uint32_t u = ((*this)() >> (64 - 23)) | 0x3F800000;
static_assert(sizeof(f) == sizeof(u), "Float and U32 must have the same size");
memcpy(&f, &u, sizeof(f)); // Note: (end-begin) * f + (2*begin-end) may fail to return a number >= // begin. return (end - begin) * (f - 1.0f) + begin;
}
// State for geometric distributions. // The stored value is inv_log_1mp using GeometricDistribution = float; static GeometricDistribution MakeGeometric(float p) { return 1.0 / std::log(1 - p);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.