/** Return the next pseudo random number, as an unsigned value of atmostbitCountbits. @parambitCountThemaximumnumberofbitstobereturned
*/
uint32_t nextBits(unsigned bitCount) {
SkASSERT(bitCount > 0 && bitCount <= 32); return this->nextU() >> (32 - bitCount);
}
/** Return the next pseudo random unsigned number, mapped to lie within [min,max]inclusive.
*/
uint32_t nextRangeU(uint32_t min, uint32_t max) {
SkASSERT(min <= max);
uint32_t range = max - min + 1; if (0 == range) { return this->nextU();
} else { return min + this->nextU() % range;
}
}
/** Return the next pseudo random unsigned number, mapped to lie within [0,count).
*/
uint32_t nextULessThan(uint32_t count) {
SkASSERT(count > 0); return this->nextRangeU(0, count - 1);
}
/** Return the next pseudo random number expressed as a SkScalar intherange[0..SK_Scalar1).
*/
SkScalar nextUScalar1() { return SkFixedToScalar(this->nextUFixed1()); }
/** Return the next pseudo random number expressed as a SkScalar intherange[min..max).
*/
SkScalar nextRangeScalar(SkScalar min, SkScalar max) { return this->nextUScalar1() * (max - min) + min;
}
/** Return the next pseudo random number expressed as a SkScalar intherange[-SK_Scalar1..SK_Scalar1).
*/
SkScalar nextSScalar1() { return SkFixedToScalar(this->nextSFixed1()); }
/** Return the next pseudo random number as a bool.
*/ bool nextBool() { return this->nextU() >= 0x80000000; }
/** A biased version of nextBool().
*/ bool nextBiasedBool(SkScalar fractionTrue) {
SkASSERT(fractionTrue >= 0 && fractionTrue <= 1); return this->nextUScalar1() <= fractionTrue;
}
/** Reset the random object.
*/ void setSeed(uint32_t seed) { init(seed); }
private: // Initialize state variables with LCG. // We must ensure that both J and K are non-zero, otherwise the // multiply-with-carry step will forevermore return zero. void init(uint32_t seed) {
fK = NextLCG(seed); if (0 == fK) {
fK = NextLCG(fK);
}
fJ = NextLCG(fK); if (0 == fJ) {
fJ = NextLCG(fJ);
}
SkASSERT(0 != fK && 0 != fJ);
} static uint32_t NextLCG(uint32_t seed) { return kMul*seed + kAdd; }
/** Return the next pseudo random number expressed as an unsigned SkFixed intherange[0..SK_Fixed1).
*/
SkFixed nextUFixed1() { return this->nextU() >> 16; }
/** Return the next pseudo random number expressed as a signed SkFixed intherange[-SK_Fixed1..SK_Fixed1).
*/
SkFixed nextSFixed1() { return this->nextS() >> 15; }
// See "Numerical Recipes in C", 1992 page 284 for these constants // For the LCG that sets the initial state from a seed enum {
kMul = 1664525,
kAdd = 1013904223
}; // Constants for the multiply-with-carry steps enum {
kKMul = 30345,
kJMul = 18000,
};
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.