1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 21:07:35 +00:00

LibTest: Add more numeric generators

Rename unsigned_int generator to number_u32.
Add generators:
- number_u64
- number_f64
- percentage
This commit is contained in:
Martin Janiczek 2023-12-30 16:23:59 +01:00 committed by Andrew Kaster
parent dd53f64d2f
commit d52ffcd830
15 changed files with 312 additions and 133 deletions

View file

@ -5,6 +5,8 @@
*/
#include <AK/Random.h>
#include <AK/UFixedBigInt.h>
#include <AK/UFixedBigIntDivision.h>
namespace AK {
@ -28,4 +30,16 @@ u32 get_random_uniform(u32 max_bounds)
return random_value % max_bounds;
}
u64 get_random_uniform_64(u64 max_bounds)
{
// Uses the same algorithm as `get_random_uniform`,
// by replacing u64 with u128 and u32 with u64.
const u64 max_usable = UINT64_MAX - static_cast<u64>((static_cast<u128>(UINT64_MAX) + 1) % max_bounds);
auto random_value = get_random<u64>();
for (int i = 0; i < 20 && random_value > max_usable; ++i) {
random_value = get_random<u64>();
}
return random_value % max_bounds;
}
}